Skip to content
Latchkey

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.

pytest
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

  1. Seed any randomness used in parametrization with a fixed value.
  2. Sort dynamic inputs so ids are stable and ordered.
  3. Re-run under xdist.
Python
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.

Terminal
pytest -n auto --dist loadscope

How 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.

Frequently asked questions

What causes ""Different tests were collected""?
Parametrize ids derived from randomness, time, or set iteration differ per worker, so the collected node IDs do not match.
How do I fix "Different tests were collected"?
Make collection deterministic

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card