35

I tried to use setUpClass() method for the first time in my life and wrote:

class TestDownload(unittest.TestCase):

    def setUpClass(cls):
        config.fs = True

and got:

Ran 0 tests in 0.004s

FAILED (errors=1)

Failure
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.5/unittest/suite.py", line 163, in _handleClassSetUp
    setUpClass()
TypeError: setUpClass() missing 1 required positional argument: 'cls'

What does it mean and how to satisfy it?

1
  • 4
    Did you put a @classmethod decorator before def setUpClass(cls): ? Apr 4, 2018 at 13:07

2 Answers 2

61

You need to put a @classmethod decorator before def setUpClass(cls).

class TestDownload(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        config.fs = True

The setupClass docs are here and classmethod docs here.

What happens is that in suite.py line 163 the setUpClass gets called on the class (not an instance) as a simple function (as opposed to a bound method). There is no argument passed silently to setUpClass, hence the error message.

By adding the @classmethod decorator, you are saying that when TestDownload.setupClass() is called, the first argument is the class TestDownload itself.

0

Adding @classmethod before setUp and tearDown will resolve the issue. @classmethod is bound to the class.

class LoginTest(unittest.TestCase):

   @classmethod
   def setUpClass(cls):

   **Your code**

   @classmethod
   def tearDownClass(self):
1
  • 1
    How does this complement the existing answer? Oct 25, 2020 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.