I have one class BaseTest and all tests are extended from it. Tests are located in different modules and packages. setUpClass and tearDownClass methods are executed before each unittest.TestCase class. How can I execute setUp and tearDown only once. Before and after all tests.
this is example of code:
import unittest
class BaseTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("setUpClass")
@classmethod
def tearDownClass(cls):
print("tearDownClass")
if __name__ == '__main__':
unittest.main()
module2.py:
class TestOne(BaseTest):
def test_one(self):
print("Test One")
class TestTwo(BaseTest):
def test_two(self):
print("Test Two")
if __name__ == '__main__':
unittest.main()
module3.py
class TestThree(BaseTest):
def test_three(self):
print("Test Three")
class TestFour(BaseTest):
def test_four(self):
print("Test Four")
if __name__ == '__main__':
unittest.main()
module4.py
class TestFive(BaseTest):
def test_five(self):
print("Test Five")
if __name__ == '__main__':
unittest.main()
its possible to do with unittest
copy the answer from https://stackoverflow.com/a/64892396/2679740 here
you can do it by defining startTestRun
, stopTestRun
of unittest.TestResult
class.
by adding following code to my tests/__init__.py
i managed to achieve it. this code runs only once for all tests(regardless of number of test classes and test files).
def startTestRun(self):
"""
https://docs.python.org/3/library/unittest.html#unittest.TestResult.startTestRun
Called once before any tests are executed.
:return:
"""
DockerCompose().start()
setattr(unittest.TestResult, 'startTestRun', startTestRun)
def stopTestRun(self):
"""
https://docs.python.org/3/library/unittest.html#unittest.TestResult.stopTestRun
Called once after all tests are executed.
:return:
"""
DockerCompose().compose.stop()
setattr(unittest.TestResult, 'stopTestRun', stopTestRun)
I don't think unittest has a facility for universal setup and teardown. You should look into pytest, its fixtures are more powerful.
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.