Worker distribution strategies help run tests faster by sharing work across multiple helpers called workers.
Worker distribution strategies in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
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).
Practice
--dist=loadscope option do in pytest-xdist worker distribution?Solution
Step 1: Understand the meaning of loadscope
Theloadscopemode groups tests by their scope, such as class or module, so related tests run together.Step 2: Compare with other distribution modes
Unlike random or file-based grouping,loadscopekeeps related tests together for better caching and setup reuse.Final Answer:
It groups tests by their scope and distributes them to workers. -> Option CQuick Check:
loadscope = group by scope [OK]
- Confusing loadscope with random distribution
- Thinking loadscope groups by file size
- Assuming loadscope runs tests sequentially
Solution
Step 1: Identify correct syntax for number of workers
The correct syntax is-n 4to specify 4 workers; spelling out 'four' is invalid.Step 2: Match distribution mode to file-based
The file-based distribution mode isloadfile, so--dist=loadfileis correct.Final Answer:
pytest -n 4 --dist=loadfile -> Option AQuick Check:
-n 4 and --dist=loadfile correct syntax [OK]
- Using spelled-out numbers like 'four'
- Mixing distribution modes incorrectly
- Using equals sign with -n option
pytest -n 3 --dist=loadfile, and three test files test_a.py, test_b.py, test_c.py, how will tests be distributed?Solution
Step 1: Understand loadfile distribution
Loadfile mode assigns tests grouped by file to different workers, so each worker gets whole files.Step 2: Match number of workers to files
With 3 workers and 3 files, each worker will get one file's tests exclusively.Final Answer:
Each worker runs tests from one file exclusively. -> Option DQuick Check:
loadfile = group by file [OK]
- Thinking tests are split randomly
- Confusing loadfile with loadscope
- Assuming tests run sequentially
pytest -n 2 --dist=loadscope but notice tests from the same class run on different workers. What is the likely cause?Solution
Step 1: Understand loadscope grouping behavior
Loadscope groups tests by scope like class or module, so tests in the same class should run together.Step 2: Identify why grouping fails
If tests from the same class run on different workers, pytest likely failed to detect the class scope properly, causing wrong grouping.Final Answer:
Tests are not properly grouped because the class scope is not detected. -> Option AQuick Check:
Undetected scope breaks loadscope grouping [OK]
- Thinking -n must be 1 for loadscope
- Believing --dist is ignored with multiple workers
- Assuming distribution is always random
Solution
Step 1: Identify the distribution mode for custom groups
Theloadgroupmode is designed for custom grouping of tests for distribution.Step 2: Understand correct command usage
Using--dist=loadgroupwith-n 3enables pytest-xdist to distribute tests based on user-defined groups configured elsewhere (e.g., in pytest hooks).Final Answer:
pytest -n 3 --dist=loadgroup -> Option BQuick Check:
loadgroup enables custom test groups [OK]
- Adding invalid --group option
- Using --tx incorrectly for grouping
- Confusing loadgroup with loadfile or loadscope
