python runtime_error ai_generated true

AssertionError: assert 0 == 3 # passes when run alone, fails in full suite

ID: python/pytest-test-order-dependency-flaky

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

Tests share module-level or database state and depend on execution order. A prior test mutates global state (e.g., a registry dict) that a later test assumes is pristine, so the test passes in isolation but fails in the full run.

generic

中文

测试共享模块级或数据库状态并依赖执行顺序。前面的测试修改了全局状态(例如注册表字典),后续测试假设它是干净的,因此单独运行通过但完整运行失败。

Workarounds

  1. 93% success
    @pytest.fixture(autouse=True)
    def reset_registry():
        registry.clear()
        yield
        registry.clear()
  2. 90% success
    @pytest.fixture
    def app():
        return App()  # fresh per test

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Disabling random ordering makes the order fixed but does not remove the shared-state dependency; it just hides it under one deterministic order.

  2. 70% fail

    Reruns may eventually pass but the underlying state leak persists and slows the suite.