Worker distribution strategies help run tests faster by sharing work across multiple helpers called workers.
Worker distribution strategies in PyTest
pytest -n <num_workers> --dist=<mode>
-n sets how many workers run tests in parallel.
--dist chooses how tests are shared: loadscope, loadfile, or loadgroup.
pytest -n 4 --dist=loadscopepytest -n 3 --dist=loadfilepytest -n 2 --dist=loadgroupThis simple test runs three checks. Using -n 2 and --dist=loadscope, pytest runs tests in parallel with 2 workers, grouping tests by their scope to balance load.
import pytest @pytest.mark.parametrize('x', [1, 2, 3]) def test_example(x): assert x > 0 # Run this command in terminal: # pytest -n 2 --dist=loadscope # This runs the tests in parallel using 2 workers, grouping tests by their scope.
Use loadscope to group tests by their class or module, which helps balance load better than just files.
loadfile sends whole test files to workers, which is simpler but may cause imbalance if files differ in size.
loadgroup requires you to define groups, useful for special cases.
Worker distribution strategies help run tests faster by sharing work across multiple workers.
Use -n to set number of workers and --dist to choose distribution mode.
Common modes are loadscope (group by scope), loadfile (group by file), and loadgroup (custom groups).