How to sort out the functions in python Unit Test
This article shows you how to organize the functions in the python unit test, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. SetUp prepares the environment.
Prerequisites for executing each test case.
2. TearDown recovers the environment.
Execute the postconditions for each test case.
3. SetUpClass all preconditions executed by case can only be run once.
You must use the @ classmethod decorator
4. TearDownClass only runs once after all case is run.
You must use the @ classmethod decorator
Example
Import unittest# inherits unittest.TestCaseclass CalcTestcase (unittest.TestCase): def setUp (self)-> None: print ("I am setUp") def test01 (self): print ("I am test method test01") def test02 (self): print ("I am test method test02") def test03 (self): print ("I am test method test03") def tearDown ( Self)-> None: print ("I am tearDown") @ classmethod def setUpClass (cls)-> None: print ("I am setUpClass") @ classmethod def tearDownClass (cls)-> None: print ("I am tearDownClass") if _ _ name__ = ='_ _ main__': unittest.main () # setup kit # suite = unittest.TestSuite () # # add test methods to the collection Then loop the value, add it to the suite and output # list = ["test01", "test02", "test03"] # for i in list: # suite.addTest (CalcTestcase (I)) the above is how to clean up the functions in the python unit test. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.