python runtime_error ai_generated true

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

ID: python/pytest-xdist-order-dependent

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-06-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active
4.x active

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 80% fail

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