python runtime_error ai_generated partial

AssertionError: assert 'foo' == 'bar' # 间歇性

AssertionError: assert 'foo' == 'bar' # intermittent

ID: python/pytest-flaky-test-random-failure

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率
    @pytest.fixture
    def state():
        return {}
    
    def test_a(state):
        state['x'] = 1
  2. 85% 成功率
    pytest -p randomly --randomly-seed=12345

无效尝试

常见但无效的做法:

  1. 70% 失败

    Masks the underlying bug and increases test time; failures may still occur.

  2. 60% 失败

    Hides the order dependency but does not fix it; CI may use a different order.