python runtime_error ai_generated true

AssertionError: assert 0 == 1 # 使用 pytest-randomly 时失败,使用 -p no:randomly 时通过

AssertionError: assert 0 == 1 # fails with pytest-randomly, passes with -p no:randomly

ID: python/pytest-test-ordering-random-seed

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

版本兼容性

版本状态引入弃用备注
3.15 active

根因分析

pytest-randomly 每次运行打乱测试顺序;顺序相关的测试或共享状态导致间歇性失败,这些失败在字母序下被隐藏。

English

pytest-randomly shuffles test order each run; order-dependent tests or shared state cause intermittent failures that were hidden by alphabetical ordering.

generic

解决方案

  1. 90% 成功率
    pytest -p randomly --randomly-seed=12345 tests/ -x
    # identify the leak, then fix with autouse reset fixture
  2. 92% 成功率
    @pytest.fixture(autouse=True)
    def reset_globals():
        myapp.state.clear()
        yield
        myapp.state.clear()

无效尝试

常见但无效的做法:

  1. 90% 失败

    Hides order dependencies and lets bugs ship; the same issue will reappear under xdist or CI parallelism.

  2. 85% 失败

    Masks the bug for that seed; other seeds still fail and the underlying leak remains.

  3. 80% 失败

    Requires pytest-ordering and creates brittle coupling; doesn't fix leaked state.