Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python unittest, how can I call a function after all tests in a TestCase have been executed?

Im running some unittests in Python and would like to call a function after all of the test cases have been run.

class MyTestCase(TestCase):
    def setUp(self):
        self.credentials = credentials

    def tearDown(self):
        print("finished running " + self._testMethodName)

    def tearDownModule(self):
        print("finished running all tests")

    def test_1(self):
        #do something

    def test_2(self):
        #do something else

setUp and tearDown are running before and after each individual test. Yet i would like to call a function after all tests are finished running (in this case test_1 and test_2).

From the documentation looks like the tearDownModule() function should do it, yet this doesnt seem to be called.

like image 598
DannyMoshe Avatar asked Sep 16 '25 15:09

DannyMoshe


1 Answers

tearDownModule is for use on the module scope, not as a method. Instead, you probably want tearDownClass:

class MyTestCase(TestCase):

    @classmethod
    def tearDownClass(cls):
        ...
like image 74
wim Avatar answered Sep 19 '25 05:09

wim