Python "pytest-randomly ordering breaks a test" in CI
pytest-randomly shuffles test order each run. A test that passes in file order but fails when shuffled has an order dependency: it relies on state another test created or left behind.
What this error means
A test fails intermittently in CI; the header prints "Using --randomly-seed=NNNN" and re-running with that seed reproduces the failure, while a different seed passes.
pytest
Using --randomly-seed=305418216
...
FAILED tests/test_orders.py::test_total - AssertionErrorCommon causes
Shared mutable module/global state
One test mutated a module-level or class-level value the failing test assumed was pristine.
Reliance on data another test created
The failing test only passes when a specific earlier test ran first and set up data.
How to fix it
Reproduce with the seed and isolate state
- Re-run with
-p randomly --randomly-seed=NNNNfrom the failing run to reproduce. - Make each test set up its own data; reset module/class globals in fixtures.
- Replace shared mutable state with function-scoped fixtures.
Terminal
pytest -p randomly --randomly-seed=305418216 tests/test_orders.py::test_totalHow to prevent it
- Keep tests independent of execution order.
- Reset or avoid shared mutable globals.
- Run pytest-randomly in CI to surface order dependence early.
Related guides
Python "IndexError: list index out of range" in CIFix "IndexError: list index out of range" in CI - test code indexed past the end of a list, often because sha…
Python "hypothesis Flaky / DeadlineExceeded" in CIFix "hypothesis.errors.Flaky" and "DeadlineExceeded" failures in CI - a property test either produced inconsi…
pytest-randomly Exposes Order-Dependent Test Failures in CIFix tests that fail only under pytest-randomly in CI - random test order revealed shared state, leaked fixtur…