# AttributeError：'TestCase' 对象没有属性 'subTest'

- **ID:** `python/pytest-subtest-in-pytest`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用 self.subTest 的 unittest.TestCase 测试被 pytest 收集，但该类没有继承自 unittest.TestCase，或导入了错误的基类（例如 pytest.TestCase 不存在）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   import unittest

class TestMath(unittest.TestCase):
    def test_add(self):
        for a, b, expected in [(1,1,2),(2,2,4)]:
            with self.subTest(a=a, b=b):
                self.assertEqual(a+b, expected)
   ```
2. **** (88% 成功率)
   ```
   pip install pytest-subtests
# then use pytest.mark.parametrize instead of subTest
   ```

## 无效尝试

- **** — Plugin helps pytest-side subtest support, but missing TestCase inheritance is the real issue. (70% 失败率)
- **** — Loses per-case reporting and stops at first failure. (75% 失败率)
- **** — Masks the class hierarchy bug. (90% 失败率)
