python runtime_error ai_generated true

FAILED tests/test_state.py::test_second_run - AssertionError:assert 2 == 1 # 单独运行通过,在 tests/test_state.py::test_first_run 之后运行则失败

FAILED tests/test_state.py::test_second_run - AssertionError: assert 2 == 1 # passes when run alone, fails when run after tests/test_state.py::test_first_run

ID: python/pytest-flaky-test-ordering

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-07-30首次发现

版本兼容性

版本状态引入弃用备注
7.x active
8.x active

根因分析

测试隔离被破坏:模块级可变对象(缓存、全局变量、类属性或数据库行)被较早的测试修改且未重置。Pytest 的默认收集顺序暴露了这种泄漏。

English

Test isolation is broken: a module-level mutable (cache, global, class attribute, or DB row) is mutated by an earlier test and not reset. Pytest's default collection order exposes the leak.

generic

解决方案

  1. 95% 成功率
    @pytest.fixture(autouse=True)
    def reset_cache():
        from myapp import cache
        cache.clear()
        yield
        cache.clear()
  2. 85% 成功率
    pip install pytest-forked
    pytest --forked tests/test_state.py
  3. 90% 成功率
    pytest --stepwise tests/test_state.py
    # or:
    pytest tests/test_state.py::test_first_run tests/test_state.py::test_second_run -v

无效尝试

常见但无效的做法:

  1. 80% 失败

    Shuffling changes which test fails, not whether tests are isolated; flakiness moves around instead of disappearing.

  2. 85% 失败

    Reruns the same broken state; if the first run mutated global state, retries still see the mutated value.