pytest-xdist "Different tests were collected between workers" in CI
pytest-xdist requires every worker to collect the identical set of tests so it can distribute them. When two workers produce different collections - from randomized parametrization or environment-dependent generation - xdist aborts.
What this error means
The run fails with "Different tests were collected between gw0 and gw1" and a diff of the collected node IDs that differ between workers.
Different tests were collected between gw0 and gw1. The difference is:
--- gw0
+++ gw1
@@ -10,7 +10,7 @@
-test_data.py::test_case[seed-41]
+test_data.py::test_case[seed-93]Common causes
Nondeterministic parametrization
Parametrize ids derived from randomness, time, or set iteration differ per worker, so the collected node IDs do not match.
Environment-dependent test generation
Tests generated from files, env vars, or directory listings that differ slightly per worker produce divergent collections.
How to fix it
Make collection deterministic
- Seed any randomness used in parametrization with a fixed value.
- Sort dynamic inputs so ids are stable and ordered.
- Re-run under xdist.
import random
random.seed(0) # stable parametrize ids across workers
@pytest.mark.parametrize("case", sorted(load_cases()))
def test_case(case): ...Use a deterministic loadgroup distribution
If generation must vary, group tests so each is assigned consistently, or disable xdist for that module.
pytest -n auto --dist loadscopeHow to prevent it
- Seed randomness used in test generation and parametrize ids.
- Sort dynamically discovered inputs for stable ordering.
- Keep collection independent of per-worker environment state.