python
type_error
ai_generated
true
AttributeError:'TestCase' 对象没有属性 'subTest'
AttributeError: 'TestCase' object has no attribute 'subTest'
ID: python/pytest-subtest-in-pytest
80%修复率
86%置信度
0证据数
2025-02-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
使用 self.subTest 的 unittest.TestCase 测试被 pytest 收集,但该类没有继承自 unittest.TestCase,或导入了错误的基类(例如 pytest.TestCase 不存在)。
English
A unittest.TestCase test using self.subTest was collected by pytest but the class doesn't inherit from unittest.TestCase, or the wrong base class was imported (e.g., pytest.TestCase doesn't exist).
解决方案
-
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) -
88% 成功率
pip install pytest-subtests # then use pytest.mark.parametrize instead of subTest
无效尝试
常见但无效的做法:
-
70% 失败
Plugin helps pytest-side subtest support, but missing TestCase inheritance is the real issue.
-
75% 失败
Loses per-case reporting and stops at first failure.
-
90% 失败
Masks the class hierarchy bug.