python type_error ai_generated true

TypeError: setUp() takes 1 positional argument but 2 were given

ID: python/unittest-setup-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-01-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.12 active

Root Cause

在 TestCase 类中定义了带参数的 setUp 方法,但 unittest 调用时不传参数

generic

中文

在 TestCase 类中定义了带参数的 setUp 方法,但 unittest 调用时不传参数

Workarounds

  1. 95% success
    def setUp(self):
  2. 70% success
    @classmethod
    def setUpClass(cls):

Dead Ends

Common approaches that don't work:

  1. 50% fail

    unittest 仍会只传 self

  2. 30% fail

    如果方法名不是 setUp,则不会被调用