python type_error ai_generated true

TypeError: TestCase.__init__() missing 1 required positional argument: 'methodName'

ID: python/unittest-testcase-requires-method

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-04-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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)
  2. 85% success
    If you must instantiate: `case = MyTest('test_something')` then `case.run(result)`.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Creates an invalid test case that fails later at run time with a different error.

  2. 70% fail

    Still raises because TestCase requires methodName.