python
data_error
ai_generated
true
FAILED tests/test_state.py::test_reads_cache - KeyError: 'user:1' (Passes with `pytest -p no:randomly`, fails with random order)
ID: python/pytest-random-order-flaky
80%Fix Rate
84%Confidence
0Evidence
2024-11-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
pytest-randomly shuffles test order each run; a test depends on state created by another test (shared cache, module global, DB row), so it fails when it runs first.
generic中文
pytest-randomly 每次运行打乱测试顺序;某测试依赖其他测试创建的状态(共享缓存、模块全局、数据库行),因此先运行时失败。
Workarounds
-
93% success
Make the test self-contained with a fixture that seeds the cache: @pytest.fixture def seeded_cache(cache): cache.set('user:1', {'name': 'alice'}) return cache def test_reads_cache(seeded_cache): assert seeded_cache.get('user:1')['name'] == 'alice' -
90% success
Or reset shared state between tests: @pytest.fixture(autouse=True) def clean_cache(cache): cache.flush_all() yield cache.flush_all()
Dead Ends
Common approaches that don't work:
-
85% fail
pytest-randomly ignores order markers unless `-p no:randomly` is set; the shuffle still happens.
-
70% fail
Makes the order deterministic but still hides the inter-test dependency; a different seed on CI breaks it.