[英]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.