{
  "id": "python/pytest-flaky-ordering-dependency",
  "signature": "FAILED tests/test_b.py::test_uses_cache - KeyError: 'user:42'\n# passes when run alone: pytest tests/test_b.py::test_uses_cache",
  "signature_zh": "FAILED tests/test_b.py::test_uses_cache - KeyError: 'user:42'\n# 单独运行时通过: pytest tests/test_b.py::test_uses_cache",
  "regex": "FAILED\\ tests/test_b\\.py::test_uses_cache\\ \\-\\ KeyError:\\ 'user:42'\\\n\\#\\ passes\\ when\\ run\\ alone:\\ pytest\\ tests/test_b\\.py::test_uses_cache",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "test_b relies on state written by test_a (module-level cache, global, or shared DB row) and pytest's default ordering runs them in file order; running in isolation or with -p randomly changes order.",
  "root_cause_type": "generic",
  "root_cause_zh": "test_b 依赖 test_a 写入的状态（模块级缓存、全局变量或共享数据库行），pytest 默认按文件顺序运行；单独运行或使用 -p 随机排序会改变顺序。",
  "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": "A fixed seed gives the same order locally but CI may use a different seed; the inter-test dependency remains a latent bug.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Couples tests by name and breaks under -p no:randomly or when parallelized with xdist.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@pytest.fixture\ndef seeded_cache(cache):\n    cache['user:42'] = {'name': 'Alice'}\n    return cache\n\ndef test_uses_cache(seeded_cache):\n    assert seeded_cache['user:42']['name'] == 'Alice'",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "# pyproject.toml\n[tool.pytest.ini_options]\naddopts = \"-p no:randomly\"  # only in local dev\n\n# CI:\npytest -p randomly --randomly-seed=random -x\n# Then refactor tests that fail only under reordering",
      "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.87,
  "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": []
}