{
  "id": "python/unittest-teardown-order-failure",
  "signature": "ERROR: tearDownClass (tests.test_db.TestDB)\n----------------------------------------------------------------------\nTraceback (most recent call last):\n  File \"tests/test_db.py\", line 30, in tearDownClass\n    cls.conn.close()\nAttributeError: 'NoneType' object has no attribute 'close'",
  "signature_zh": "ERROR: tearDownClass (tests.test_db.TestDB)\n----------------------------------------------------------------------\nTraceback (最近调用最后):\n  文件 \"tests/test_db.py\"，第 30 行，在 tearDownClass 中\n    cls.conn.close()\nAttributeError: 'NoneType' 对象没有属性 'close'",
  "regex": "ERROR:\\ tearDownClass\\ \\(tests\\.test_db\\.TestDB\\)\\\n\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\\nTraceback\\ \\(most\\ recent\\ call\\ last\\):\\\n\\ \\ File\\ \"tests/test_db\\.py\",\\ line\\ 30,\\ in\\ tearDownClass\\\n\\ \\ \\ \\ cls\\.conn\\.close\\(\\)\\\nAttributeError:\\ 'NoneType'\\ object\\ has\\ no\\ attribute\\ 'close'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "setUpClass failed earlier (e.g., DB connection refused), so cls.conn was never assigned. unittest still runs tearDownClass, which then dereferences None.",
  "root_cause_type": "generic",
  "root_cause_zh": "setUpClass 之前失败了（例如数据库连接被拒绝），因此 cls.conn 从未被赋值。unittest 仍会运行 tearDownClass，然后解引用 None。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Silences the teardown error but leaves real resources (sockets, files) leaked when setUpClass partially succeeded.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Reconnects per test, dramatically slowing the suite; also does not fix the case where setUpClass itself fails.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@classmethod\ndef tearDownClass(cls):\n    if getattr(cls, 'conn', None) is not None:\n        cls.conn.close()\n        cls.conn = None",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "@classmethod\ndef setUpClass(cls):\n    cls.conn = connect()\n    cls.addClassCleanup(cls.conn.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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-04-27",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}