简体   繁体   English

在Python unittest中,执行完TestCase中的所有测试后如何调用函数?

[英]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. 我在Python中运行一些单元测试,并且想在所有测试用例都运行完之后调用一个函数。

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. setUp和tearDown在每个单独的测试之前和之后运行。 Yet i would like to call a function after all tests are finished running (in this case test_1 and test_2). 但是我想在所有测试完成运行后调用一个函数(在本例中为test_1和test_2)。

From the documentation looks like the tearDownModule() function should do it, yet this doesnt seem to be called. 从文档中看来,tearDownModule()函数应该执行此操作,但似乎并未调用它。

tearDownModule is for use on the module scope, not as a method. tearDownModule用于模块范围,而不是方法。 Instead, you probably want tearDownClass : 相反,您可能需要tearDownClass

class MyTestCase(TestCase):

    @classmethod
    def tearDownClass(cls):
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PYTHON-函数执行后如何删除? - PYTHON - How do I remove a function after it's been executed? Python unittest-在创建带有特定测试的测试套件之后,我所有的测试都将执行 - Python unittest - after creating test suite with particular test all my tests are being executed Python unittest 仅在执行所有 class 测试时失败 - Python unittest only fails when all class tests are executed 如何为所有 unittest.TestCase 类执行 tearDown 和 setUp 方法 - How can I execute tearDown and setUp method for all unittest.TestCase classes 如何使用unittest在并行的Selenium Python测试中执行 - How can I execute in parallel Selenium Python tests with unittest Python unittest - 如何选择执行测试的url? - Python unittest - how to chose the url on which the tests are executed? python响应-并非所有请求都已执行 - python responses - Not all requests have been executed 断言函数不在Python unittest中执行 - Assert a function is not executed in Python unittest 如何在执行函数后获取函数的局部值? - How can I get the values of the locals of a function after it has been executed? Python Webdriver我定义了一个类,但是从unittest TestCase类中我看不到类方法 - Python Webdriver I have a class defined but from the unittest TestCase class I cannot see the class methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM