简体   繁体   中英

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.

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

class MyTestCase(TestCase):

    @classmethod
    def tearDownClass(cls):
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM