{
  "id": "python/unittest-teardown-attribute-error",
  "signature": "AttributeError: 'TestUser' object has no attribute 'db'",
  "signature_zh": "AttributeError: 'TestUser' 对象没有属性 'db'",
  "regex": "AttributeError:\\ 'TestUser'\\ object\\ has\\ no\\ attribute\\ 'db'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "setUpClass or setUp failed silently (or was misspelled) so the attribute assigned in setUp is never created, but tearDown still references it.",
  "root_cause_type": "generic",
  "root_cause_zh": "setUpClass 或 setUp 静默失败（或拼写错误），导致 setUp 中赋值的属性从未创建，但 tearDown 仍引用它。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "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": "Hides the real setup failure; subsequent tests may run against a half-initialized state.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If setUp is the failing method, referencing the attribute earlier just moves the error.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "class TestUser(unittest.TestCase):\n    def setUp(self):\n        self.db = connect()\n\n    def tearDown(self):\n        if hasattr(self, 'db'):\n            self.db.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "def setUp(self):\n    self.db = connect()\n    self.addCleanup(self.db.close)",
      "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-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}