python
type_error
ai_generated
true
TypeError: TestCase.__init__() missing 1 required positional argument: 'methodName'
ID: python/unittest-testcase-requires-method
80%Fix Rate
86%Confidence
0Evidence
2024-04-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
Instantiating a `unittest.TestCase` subclass directly (e.g. `MyTest()`) instead of letting the test runner instantiate it, or forgetting to pass a method name.
generic中文
直接实例化 `unittest.TestCase` 子类(如 `MyTest()`),而不是让测试运行器实例化,或忘记传入方法名。
Workarounds
-
95% success
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% success
If you must instantiate: `case = MyTest('test_something')` then `case.run(result)`.
Dead Ends
Common approaches that don't work:
-
60% fail
Creates an invalid test case that fails later at run time with a different error.
-
70% fail
Still raises because TestCase requires methodName.