Test Overview
This test checks that pytest distributes test cases evenly across multiple workers when using the pytest-xdist plugin. It verifies that each worker runs a subset of tests and all tests complete successfully.
Jump into concepts and practice - no test required
This test checks that pytest distributes test cases evenly across multiple workers when using the pytest-xdist plugin. It verifies that each worker runs a subset of tests and all tests complete successfully.
import pytest @pytest.mark.parametrize('num', range(6)) def test_sample(num): assert num >= 0 # Run command: pytest -n 3 # This runs tests distributed across 3 workers
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test run starts with pytest-xdist plugin enabled using 3 workers | Terminal shows pytest starting with 3 parallel workers | — | PASS |
| 2 | Pytest collects 6 test cases from test_sample function | Test collection complete, 6 tests found | Verify total tests collected equals 6 | PASS |
| 3 | Tests are distributed evenly across 3 workers (2 tests per worker) | Each worker assigned 2 tests to run | Check that each worker runs exactly 2 tests | PASS |
| 4 | Each worker executes its assigned tests in parallel | Workers running tests concurrently | Each test assertion (num >= 0) passes | PASS |
| 5 | All workers complete tests and report results | Terminal shows all 6 tests passed | Verify all tests passed with no failures | PASS |
--dist=loadscope option do in pytest-xdist worker distribution?loadscope mode groups tests by their scope, such as class or module, so related tests run together.loadscope keeps related tests together for better caching and setup reuse.-n 4 to specify 4 workers; spelling out 'four' is invalid.loadfile, so --dist=loadfile is correct.pytest -n 3 --dist=loadfile, and three test files test_a.py, test_b.py, test_c.py, how will tests be distributed?pytest -n 2 --dist=loadscope but notice tests from the same class run on different workers. What is the likely cause?loadgroup mode is designed for custom grouping of tests for distribution.--dist=loadgroup with -n 3 enables pytest-xdist to distribute tests based on user-defined groups configured elsewhere (e.g., in pytest hooks).