The unit test process in Python
Looked at the unit test flow in Python, wrote a test code to see the overall testing process
Summarized as follows
- A test case class should derive from UnitTest. TestCase
- The normal test case is called in the order
- Subclass TestCase can register its own cleanup function (My_cleanup). This cleanup function will be called after teardown
def setup (self): super (Mytestcase, self). Setup () self.addcleanup (Self.my_cleanup)
- Unit test function Name must start with "Test_"
def test_case_1 (self): print ("++++ test Case 1")
Test the source code:
From __future__ import print_functionimport unittestdef setupmodule (): print ("Setupmodule") def teardownmodule (): print ("Teardownmodule") class Mytestcase (unittest. TestCase): def setup (self): super (Mytestcase, self). Setup () self.addcleanup (self.my_cleanup) Print () print ("++++ setup") def TearDown (self): super (Mytestcase, self). TearDown () print ("++++ TearDown ") raise Exception () def my_cleanup (self): print (" ++++ my_cleanup ") def test_case_1 ( Self): print (' ++++ test Case 1 ') def test_case_2 (self): print ("++++ test Case 2") @classmethod def setupclass (CLS): print ("SetupClass") @classmethod def teardownclass (CLS): print () print ("Teardownclass")
The running result of this program:
setupmodulesetupclass++++ setup++++ test Case 1++++ teardown++++ my_cleanup++++ setup++++ test Case 2++++ tearDown++++ my_ Cleanupteardownclassteardownmodule
The unit test process in Python