Skip to content

Instantly share code, notes, and snippets.

@joshprice
Last active July 2, 2024 05:10
Show Gist options
  • Save joshprice/864ca419c6e8f47000757977f3ff6db2 to your computer and use it in GitHub Desktop.
Save joshprice/864ca419c6e8f47000757977f3ff6db2 to your computer and use it in GitHub Desktop.
Buildkite Python Test Collector with xdist
def add(a, b):
return a + b
def subtract(a, b):
return a - b
[pytest]
log_cli = True
log_cli_level = DEBUG
pytest
pytest-xdist
buildkite-test-collector
import pytest
import time
class TestClassOne:
def test_one(self):
time.sleep(0.5) # Simulate some work
assert True
def test_two(self):
time.sleep(0.4) # Simulate more work
assert 1 == 1
def test_two_again(self):
time.sleep(0.3) # Simulate some work
assert "a" in "abc"
def test_one_again(self):
time.sleep(0.6) # Simulate very quick work
assert isinstance([], list)
from main import add, subtract
def test_add():
assert add(2, 3) == 5
def test_subtract():
assert subtract(5, 3) == 2
@joshprice
Copy link
Author

joshprice commented Jul 2, 2024

Test run with -n 2 and 6 tests, 2 in functional style, 4 in class style. No duplicates are seen, API payload is only sent once.

BUILDKITE_ANALYTICS_DEBUG_ENABLED=TRUE BUILDKITE_ANALYTICS_TOKEN="xyz" CI=TRUE pytest -v --capture=no -n 2
************ env RuntimeEnvironment(ci='generic', key='b78973c4-a432-46c0-9b61-38504380b3fe', number=None, job_id=None, branch=None, commit_sha=None, message=None, url=None)
========================================== test session starts ==========================================
platform darwin -- Python 3.12.3, pytest-8.2.2, pluggy-1.5.0 -- /Users/josh/dev/buildkite/test-collector-python/bkenv/bin/python3.12
cachedir: .pytest_cache
rootdir: /Users/josh/dev/buildkite/parallel-test-demo
configfile: pytest.ini
plugins: buildkite-test-collector-0.1.7, xdist-3.6.1
2 workers [6 items]
scheduling tests via LoadScheduling

test_second.py::TestClassOne::test_one
test_main.py::test_add
TestData(id=UUID('2e207350-4e41-4f00-a921-f5e8240bfb8e'), scope='test_main.py', name='test_add', history=TestHistory(start_at=Instant(seconds=361051.745951708), end_at=None, duration=None, children=()), location='test_main.py:2', result=None)

[gw0] PASSED test_main.py::test_add
test_main.py::test_subtract
TestData(id=UUID('0fb9c551-c4a0-4c39-83d9-84c97201172b'), scope='test_main.py', name='test_subtract', history=TestHistory(start_at=Instant(seconds=361051.746708541), end_at=None, duration=None, children=()), location='test_main.py:5', result=None)

[gw0] PASSED test_main.py::test_subtract
test_second.py::TestClassOne::test_two_again
TestData(id=UUID('0dca3bf4-6bca-45bb-8421-71ef7a48d2aa'), scope='test_second.py::TestClassOne', name='test_two_again', history=TestHistory(start_at=Instant(seconds=361051.747351208), end_at=None, duration=None, children=()), location='test_second.py:12', result=None)

[gw0] PASSED test_second.py::TestClassOne::test_two_again
test_second.py::TestClassOne::test_one_again
TestData(id=UUID('a823b14d-7a6a-4c03-8e2b-35b2d01518a5'), scope='test_second.py::TestClassOne', name='test_one', history=TestHistory(start_at=Instant(seconds=361051.745741708), end_at=None, duration=None, children=()), location='test_second.py:4', result=None)

[gw1] PASSED test_second.py::TestClassOne::test_one
test_second.py::TestClassOne::test_two
TestData(id=UUID('d82e714a-47bf-4726-92c5-d35794d60236'), scope='test_second.py::TestClassOne', name='test_two', history=TestHistory(start_at=Instant(seconds=361052.253358208), end_at=None, duration=None, children=()), location='test_second.py:8', result=None)

[gw1] PASSED test_second.py::TestClassOne::test_two
TestData(id=UUID('209e1735-a421-493c-9a2c-7b92034cb9be'), scope='test_second.py::TestClassOne', name='test_one_again', history=TestHistory(start_at=Instant(seconds=361052.061855583), end_at=None, duration=None, children=()), location='test_second.py:16', result=None)

[gw0] PASSED test_second.py::TestClassOne::test_one_again

=========================================== 6 passed in 1.43s ===========================================
DEBUG MODE ON TRUE
{ 'data': ( TestData(id=UUID('2e207350-4e41-4f00-a921-f5e8240bfb8e'),
                     scope='test_main.py',
                     name='test_add',
                     history=TestHistory(start_at=Instant(seconds=361051.745951708),
                                         end_at=Instant(seconds=361051.7464),
                                         duration=datetime.timedelta(microseconds=448),
                                         children=()),
                     location='test_main.py:2',
                     result=TestResultPassed()),
            TestData(id=UUID('0fb9c551-c4a0-4c39-83d9-84c97201172b'),
                     scope='test_main.py',
                     name='test_subtract',
                     history=TestHistory(start_at=Instant(seconds=361051.746708541),
                                         end_at=Instant(seconds=361051.747024083),
                                         duration=datetime.timedelta(microseconds=316),
                                         children=()),
                     location='test_main.py:5',
                     result=TestResultPassed()),
            TestData(id=UUID('0dca3bf4-6bca-45bb-8421-71ef7a48d2aa'),
                     scope='test_second.py::TestClassOne',
                     name='test_two_again',
                     history=TestHistory(start_at=Instant(seconds=361051.747351208),
                                         end_at=Instant(seconds=361052.058194875),
                                         duration=datetime.timedelta(microseconds=310844),
                                         children=()),
                     location='test_second.py:12',
                     result=TestResultPassed()),
            TestData(id=UUID('a823b14d-7a6a-4c03-8e2b-35b2d01518a5'),
                     scope='test_second.py::TestClassOne',
                     name='test_one',
                     history=TestHistory(start_at=Instant(seconds=361051.745741708),
                                         end_at=Instant(seconds=361052.251923166),
                                         duration=datetime.timedelta(microseconds=506181),
                                         children=()),
                     location='test_second.py:4',
                     result=TestResultPassed()),
            TestData(id=UUID('d82e714a-47bf-4726-92c5-d35794d60236'),
                     scope='test_second.py::TestClassOne',
                     name='test_two',
                     history=TestHistory(start_at=Instant(seconds=361052.253358208),
                                         end_at=Instant(seconds=361052.660601625),
                                         duration=datetime.timedelta(microseconds=407243),
                                         children=()),
                     location='test_second.py:8',
                     result=TestResultPassed()),
            TestData(id=UUID('209e1735-a421-493c-9a2c-7b92034cb9be'),
                     scope='test_second.py::TestClassOne',
                     name='test_one_again',
                     history=TestHistory(start_at=Instant(seconds=361052.061855583),
                                         end_at=Instant(seconds=361052.664773916),
                                         duration=datetime.timedelta(microseconds=602918),
                                         children=()),
                     location='test_second.py:16',
                     result=TestResultPassed())),
  'finished_at': None,
  'run_env': RuntimeEnvironment(ci='generic',
                                key='b78973c4-a432-46c0-9b61-38504380b3fe',
                                number=None,
                                job_id=None,
                                branch=None,
                                commit_sha=None,
                                message=None,
                                url=None),
  'started_at': Instant(seconds=361051.745691291)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment