python runtime_error ai_generated true

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

FAILED tests/test_state.py::test_b - AssertionError: assert [] == [1, 2] # passes when run alone, fails when run after test_a

ID: python/pytest-randomly-fails-order-dependent

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

版本兼容性

版本状态引入弃用备注
3.x active — — —
4.x active — — —

根因分析

pytest-randomly 打乱测试顺序,暴露了模块级列表、类属性或全局缓存等共享可变状态在测试之间泄漏的问题。

English

pytest-randomly shuffles test order and exposes shared mutable state (module-level list, class attribute, or global cache) that leaks between tests.

generic

解决方案

  1. 95% 成功率
    import pytest
    
    @pytest.fixture(autouse=True)
    def reset_state():
        import myapp.cache
        myapp.cache.clear()
        yield
        myapp.cache.clear()
  2. 90% 成功率
    @pytest.fixture
    def items():
        return []
    
    def test_a(items):
        items.append(1)
    
    def test_b(items):
        assert items == []  # always fresh

无效尝试

常见但无效的做法:

  1. 90% 失败

    Hides the state leak; the bug returns in production or when order changes in CI.

  2. 85% 失败

    Reproduces the same order but does not fix the leaking state.