# AttributeError: 类型对象 'TestDb' 没有属性 'client'
  File "tests/test_db.py", line 22, in test_query
    resp = self.client.get('/health')

- **ID:** `python/unittest-setupclass-attribute-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

`setUpClass` 是类方法；在其中赋值 `self.client` 设置的是类对象上的实例属性，而非测试实例上的属性，因此测试实例上的 `self.client` 不存在。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.10 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Assign on the class explicitly:
@classmethod
def setUpClass(cls):
    cls.client = TestClient(app)
    cls._server = start_server()
   ```
2. **** (88% 成功率)
   ```
   Or use a class-scoped pytest fixture instead:
@pytest.fixture(scope='class')
def client():
    return TestClient(app)
   ```

## 无效尝试

- **** — The attribute exists but is None; calling `.get` raises AttributeError on NoneType. (80% 失败率)
- **** — Works but recreates the client for every test, defeating the purpose of setUpClass and slowing the suite. (50% 失败率)
