python
runtime_error
ai_generated
true
AssertionError: assert 0 == 3 # 单独运行时通过,完整套件运行时失败
AssertionError: assert 0 == 3 # passes when run alone, fails in full suite
ID: python/pytest-test-order-dependency-flaky
80%修复率
85%置信度
0证据数
2024-12-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
测试共享模块级或数据库状态并依赖执行顺序。前面的测试修改了全局状态(例如注册表字典),后续测试假设它是干净的,因此单独运行通过但完整运行失败。
English
Tests share module-level or database state and depend on execution order. A prior test mutates global state (e.g., a registry dict) that a later test assumes is pristine, so the test passes in isolation but fails in the full run.
解决方案
-
93% 成功率
@pytest.fixture(autouse=True) def reset_registry(): registry.clear() yield registry.clear() -
90% 成功率
@pytest.fixture def app(): return App() # fresh per test
无效尝试
常见但无效的做法:
-
60% 失败
Disabling random ordering makes the order fixed but does not remove the shared-state dependency; it just hides it under one deterministic order.
-
70% 失败
Reruns may eventually pass but the underlying state leak persists and slows the suite.