{
  "id": "python/pytest-monkeypatch-not-undone",
  "signature": "AssertionError: assert <MagicMock> == '/tmp/real'\n# first test passes, second fails when run together",
  "signature_zh": "AssertionError: assert <MagicMock> == '/tmp/real'\n# 单独运行时第一个测试通过，一起运行时第二个失败",
  "regex": "AssertionError:\\ assert\\ <MagicMock>\\ ==\\ '/tmp/real'\\\n\\#\\ first\\ test\\ passes,\\ second\\ fails\\ when\\ run\\ together",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A test mutates a global (env var, module attribute, dict) without using monkeypatch or addfinalizer, so the change leaks into subsequent tests.",
  "root_cause_type": "generic",
  "root_cause_zh": "某个测试修改了全局对象（环境变量、模块属性、字典），但未使用 monkeypatch 或 addfinalizer，导致变更泄漏到后续测试。",
  "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": "Fragile and unpredictable; pytest ordering depends on collection and xdist distribution.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the test fails before finally, or uses yield fixtures incorrectly, restoration still may not happen.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Only helps if pytest-randomly is the cause; the underlying leak persists.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "def test_x(monkeypatch):\n    monkeypatch.setenv('APP_MODE', 'test')\n    monkeypatch.setattr('myapp.config.DEBUG', True)\n    run()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "@pytest.fixture\ndef temp_registry():\n    old = myapp.registry.copy()\n    yield myapp.registry\n    myapp.registry.clear()\n    myapp.registry.update(old)",
      "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.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}