{
  "id": "python/pytest-mock-reset-between-tests",
  "signature": "AssertionError: Expected 'mock_method' to be called once. Called 2 times.",
  "signature_zh": "AssertionError: 预期 'mock_method' 被调用一次。实际调用 2 次。",
  "regex": "AssertionError:\\ Expected\\ 'mock_method'\\ to\\ be\\ called\\ once\\.\\ Called\\ 2\\ times\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The mock was not reset between tests, so calls from a previous test are still recorded. This often happens when using a module-level mock.",
  "root_cause_type": "generic",
  "root_cause_zh": "mock 在测试之间未重置，因此前一个测试的调用仍被记录。这通常在使用模块级 mock 时发生。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This works but is error-prone and must be remembered for every test.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the mock is patching a module, it must be created in a fixture.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "def test_foo(mocker):\n    mock = mocker.patch('module.func')\n    # mocker automatically resets after test",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@pytest.fixture\ndef mock_func():\n    with patch('module.func') as m:\n        yield m",
      "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-06-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}