python runtime_error ai_generated true

AssertionError: assert 0 == 1 # 使用 -p no:xdist 通过,使用 -n auto 失败

AssertionError: assert 0 == 1 # passes with -p no:xdist, fails with -n auto

ID: python/pytest-xdist-order-dependent

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

版本兼容性

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

根因分析

测试共享全局状态(模块级字典、单例或数据库行),一个测试修改后被另一个测试依赖。pytest-xdist 将测试分发到不同 worker,破坏了隐式顺序。

English

Tests share global state (module-level dict, singleton, or DB row) that is mutated by one test and relied upon by another. pytest-xdist distributes tests across workers, breaking the implicit ordering.

generic

解决方案

  1. 92% 成功率
    @pytest.fixture(autouse=True)
    def reset_state():
        mypkg.cache.clear()
        yield
        mypkg.cache.clear()
  2. 80% 成功率
    pytest -n auto --dist=loadscope

无效尝试

常见但无效的做法:

  1. 70% 失败

    Hides the underlying state leak and defeats the purpose of using xdist for speed.

  2. 80% 失败

    Shuffling makes the flakiness worse and harder to reproduce, not better.