{
  "id": "python/unittest-setupclass-attribute-error",
  "signature": "AttributeError: type object 'TestDb' has no attribute 'client'\n  File \"tests/test_db.py\", line 22, in test_query\n    resp = self.client.get('/health')",
  "signature_zh": "AttributeError: 类型对象 'TestDb' 没有属性 'client'\n  File \"tests/test_db.py\", line 22, in test_query\n    resp = self.client.get('/health')",
  "regex": "AttributeError:\\ type\\ object\\ 'TestDb'\\ has\\ no\\ attribute\\ 'client'\\\n\\ \\ File\\ \"tests/test_db\\.py\",\\ line\\ 22,\\ in\\ test_query\\\n\\ \\ \\ \\ resp\\ =\\ self\\.client\\.get\\('/health'\\)",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "`setUpClass` is a classmethod; assigning `self.client` inside it sets an instance attribute on the class object, not on instances, so `self.client` on a test instance is missing.",
  "root_cause_type": "generic",
  "root_cause_zh": "`setUpClass` 是类方法；在其中赋值 `self.client` 设置的是类对象上的实例属性，而非测试实例上的属性，因此测试实例上的 `self.client` 不存在。",
  "versions": [
    {
      "version": "3.10",
      "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": "The attribute exists but is None; calling `.get` raises AttributeError on NoneType.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works but recreates the client for every test, defeating the purpose of setUpClass and slowing the suite.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Assign on the class explicitly:\n@classmethod\ndef setUpClass(cls):\n    cls.client = TestClient(app)\n    cls._server = start_server()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "Or use a class-scoped pytest fixture instead:\n@pytest.fixture(scope='class')\ndef client():\n    return TestClient(app)",
      "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-25",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}