A, unittest primary use
Unit testing in four core concepts: test case (test case), test suite (Test Suite), test runner (test runner), test fixture (test environment data preparation and clean-up or test data scaffolding)
※ test case: an instance of a testcase is a test case. Before the test set up (setUp) prepared environment, test code execution (run), and the reducing environment of the test (tearDown)
※ TestSuite: more test cases together.
※ TestLoader: Testcase used to load into the TestSuite.
※ TextTestRunner: used to execute test cases, wherein the run (test) performs TestSuite / TestCase the run (result) method.
※ test fixture: test environment to build and destroy
1. When creating a new class that inherits unittest.TestCase. When overriding setUp and tearDown methods, then call unittest.main () execution cases, setUp and tearDown will perform before and after each use case:
Import unittest class TestBaidu (unittest.TestCase): # each perform a use case will execute setup () and teardown () method # If you run all use cases, and run only once a prerequisite for ending conditions. With the setUpClass () and teardownclass () DEF the setUp (Self): Print ( " pre-test initialization is performed ======== " ) DEF the tearDown (Self): Print ( " After completion of the test case execution finishing operation ================ " ) DEF test_add (Self): Print (+. 5. 7 ) DEF test_sub (Self): Print (3-2 ) IF the __name__ == ' __main__' : Unittest.main () result: >> Results: the initialization operation prior to test case execution ======== 12 test operation after finishing executing the ===== the initialization operation performed before the test ======== a finishing operation after executing the test ===== <<
2. Set setupClass and teardownClass: perform all test cases, only once initialization condition and end condition (see the following code, two test performed once only):
Import unittest class TestBaidu (unittest.TestCase): # If you run all use cases, and run only once a prerequisite for ending conditions. The use setupclass () and teardownclass () @classmethod DEF setUpClass (CLS): Print ( " before all the test cases executed only once ============ " ) @classmethod DEF tearDownClass ( CLS): Print ( " after execution of all test cases, only once ============ " ) DEF test_add (Self): Print (+. 5. 7 ) DEF test_sub (Self): Print (3-2 ) # python3 write do not write will be executed if __name__ == ' __main__ ' : unittest.main () operation results: >> Testing Started AT 22:19 ... before all the test cases are performed only once ============ 12 1 after all the test cases are performed only once ============ <<
Two, testsuit use
the unittest Import class TestBaidu (of unittest.TestCase): DEF Test01 (Self): Print ( 'Test01 test ...') DEF Test02 (Self): Print ( 'Test02 test ...') DEF TEST03 (Self): Print ( 'test03 test ...') IF __name__ == '__main__': SUIT = unittest.TestSuite () # create a test set suit.addTest (TestBaidu ( 'test01') ) # test set to add test cases to be run suit.addTest (TestBaidu ( 'Test02')) # suit.addTest ([TestBaidu ( 'Test01'), TestBaidu ( 'Test02')]) Runner = unittest.TextTestRunner () is used to perform test # runner.run (suit)
Third, all the use cases in unittest.TestLoader.discover matching directory, use case name in the directory needs to begin with a test
Import the unittest Import OS # instantiated objects Test Suite SUIT = unittest.TestSuite () # instantiated object TestLoader Loader = unittest.TestLoader () # all test files in a directory using Discover () to find and return add data to the test suite. s.addTests (loader.discover (The os.getcwd ())) RUN = unittest.TextTestRunner () run.run (S)
PS: find this article useful friends, a lot like a reward point oh!