python
runtime_error
ai_generated
true
FAILED tests/test_b.py::test_uses_cache - KeyError: 'user:42' # passes when run alone: pytest tests/test_b.py::test_uses_cache
ID: python/pytest-flaky-ordering-dependency
80%Fix Rate
87%Confidence
0Evidence
2024-07-30First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
test_b relies on state written by test_a (module-level cache, global, or shared DB row) and pytest's default ordering runs them in file order; running in isolation or with -p randomly changes order.
generic中文
test_b 依赖 test_a 写入的状态(模块级缓存、全局变量或共享数据库行),pytest 默认按文件顺序运行;单独运行或使用 -p 随机排序会改变顺序。
Workarounds
-
95% success
@pytest.fixture def seeded_cache(cache): cache['user:42'] = {'name': 'Alice'} return cache def test_uses_cache(seeded_cache): assert seeded_cache['user:42']['name'] == 'Alice' -
85% success
# pyproject.toml [tool.pytest.ini_options] addopts = "-p no:randomly" # only in local dev # CI: pytest -p randomly --randomly-seed=random -x # Then refactor tests that fail only under reordering
Dead Ends
Common approaches that don't work:
-
80% fail
A fixed seed gives the same order locally but CI may use a different seed; the inter-test dependency remains a latent bug.
-
75% fail
Couples tests by name and breaks under -p no:randomly or when parallelized with xdist.