python
runtime_error
ai_generated
partial
AssertionError: assert 'foo' == 'bar' # 间歇性
AssertionError: assert 'foo' == 'bar' # intermittent
ID: python/pytest-flaky-test-random-failure
80%修复率
80%置信度
0证据数
2024-12-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
测试依赖于顺序或共享可变状态;当以不同顺序运行(例如使用 `-p no:randomly` 或 xdist)时,它会失败。
English
Test depends on ordering or shared mutable state; when run in a different order (e.g., with `-p no:randomly` or xdist), it fails.
解决方案
-
95% 成功率
@pytest.fixture def state(): return {} def test_a(state): state['x'] = 1 -
85% 成功率
pytest -p randomly --randomly-seed=12345
无效尝试
常见但无效的做法:
-
70% 失败
Masks the underlying bug and increases test time; failures may still occur.
-
60% 失败
Hides the order dependency but does not fix it; CI may use a different order.