python
type_error
ai_generated
true
TypeError: TestCase.__init__() 缺少 1 个必需的位置参数: 'methodName'
TypeError: TestCase.__init__() missing 1 required positional argument: 'methodName'
ID: python/unittest-testcase-requires-method
80%修复率
86%置信度
0证据数
2024-04-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
直接实例化 `unittest.TestCase` 子类(如 `MyTest()`),而不是让测试运行器实例化,或忘记传入方法名。
English
Instantiating a `unittest.TestCase` subclass directly (e.g. `MyTest()`) instead of letting the test runner instantiate it, or forgetting to pass a method name.
解决方案
-
95% 成功率
Use the test runner instead of instantiating directly: import unittest unittest.main() # or loader = unittest.TestLoader() suite = loader.loadTestsFromTestCase(MyTest) unittest.TextTestRunner().run(suite)
-
85% 成功率
If you must instantiate: `case = MyTest('test_something')` then `case.run(result)`.
无效尝试
常见但无效的做法:
-
60% 失败
Creates an invalid test case that fails later at run time with a different error.
-
70% 失败
Still raises because TestCase requires methodName.