{
  "id": "python/pytest-flaky-test-ordering",
  "signature": "FAILED tests/test_state.py::test_second_run - AssertionError: assert 2 == 1\n# passes when run alone, fails when run after tests/test_state.py::test_first_run",
  "signature_zh": "FAILED tests/test_state.py::test_second_run - AssertionError：assert 2 == 1\n# 单独运行通过，在 tests/test_state.py::test_first_run 之后运行则失败",
  "regex": "FAILED\\ tests/test_state\\.py::test_second_run\\ \\-\\ AssertionError:\\ assert\\ 2\\ ==\\ 1\\\n\\#\\ passes\\ when\\ run\\ alone,\\ fails\\ when\\ run\\ after\\ tests/test_state\\.py::test_first_run",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Test isolation is broken: a module-level mutable (cache, global, class attribute, or DB row) is mutated by an earlier test and not reset. Pytest's default collection order exposes the leak.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试隔离被破坏：模块级可变对象（缓存、全局变量、类属性或数据库行）被较早的测试修改且未重置。Pytest 的默认收集顺序暴露了这种泄漏。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Shuffling changes which test fails, not whether tests are isolated; flakiness moves around instead of disappearing.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Reruns the same broken state; if the first run mutated global state, retries still see the mutated value.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@pytest.fixture(autouse=True)\ndef reset_cache():\n    from myapp import cache\n    cache.clear()\n    yield\n    cache.clear()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "pip install pytest-forked\npytest --forked tests/test_state.py",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "pytest --stepwise tests/test_state.py\n# or:\npytest tests/test_state.py::test_first_run tests/test_state.py::test_second_run -v",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-07-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}