Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittest - setUpClass() is giving me trouble - why can't I inherit like this?

I have unittest code like the following:

import unittest  class MyUnitTest(unittest.TestCase):     def setUpClass(self):         do_something_expensive_for_all_sets_of_tests()  class MyFirstSetOfTests(MyUnitTest):     def setUpClass(self):         super(MyFirstSetOfTests, self).setUpClass()         do_something_expensive_for_just_these_first_tests()      def test_one(self):         ...      def test_two(self):         ...  class MySecondSetOfTests(MyUnitTest):     def setUpClass(self):         super(MySecondSetOfTests, self).setUpClass()         do_something_expensive_for_just_these_second_tests()      def test_one(self):         ...      def test_two(self):         ...  if __name__ == '__main__':     unittest.main() 

When I try to run this code, I get an error like this:

====================================================================== ERROR: setUpClass (__main__.MyFirstSetOfTests) ---------------------------------------------------------------------- TypeError: unbound method setUpClass() must be called with MyFirstSetOfTests instance as first argument (got nothing instead)  ---------------------------------------------------------------------- 
like image 761
tadasajon Avatar asked Dec 26 '12 18:12

tadasajon


People also ask

How do you handle exceptions in unit test Python?

There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.

Which error in Python will stop a unit test abruptly?

An exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.

How do you make a test case fail in Python?

You wouldn't want to check the result body of a rest API if the result code is not the expected, for example. From the python documentation: assertEqual(first, second, msg=None) Test that first and second are equal. If the values do not compare equal, the test will fail.

How are test failures identified in Python?

If the test fails, an exception will be raised with an explanatory message, and unittest will identify the test case as a failure. Any other exceptions will be treated as errors.


1 Answers

setUpClass must be a class method. From the documentation:

A class method called before tests in an individual class run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod def setUpClass(cls):     ... 

See Class and Module Fixtures for more details.

Your version is missing the @classmethod decorator:

class MyUnitTest(unittest.TestCase):     @classmethod     def setUpClass(cls):         do_something_expensive_for_all_sets_of_tests()  class MyFirstSetOfTests(MyUnitTest):     @classmethod     def setUpClass(cls):         super(MyFirstSetOfTests, cls).setUpClass()         do_something_expensive_for_just_these_first_tests() 

The error is thrown because MyFirstSetOfTests.setUpClass() is called on the class, not on an instance, but you didn't mark your method as a classmethod and thus it was not passed in the automatic self argument. In the above updated code I used cls instead, to reflect that the name references the class object.

like image 74
Martijn Pieters Avatar answered Sep 21 '22 13:09

Martijn Pieters