python Miscellany -unittest

  • setUpModule / tearDownModule: to be performed at the beginning and end of the whole module.
  • setUpClass / tearDownClass: to be performed at the beginning and end of the test class.
  • setUp / tearDown: to be performed at the beginning and end of the test case
    Note: setUpClass / tearDownClass slightly different wording, the first parameter decorative, followed by the method of cls @classmethod, it may also be something else. Each of the above should be carried out decoration
the unittest Import 

class the MyTest (of unittest.TestCase): # Inheritance of unittest.TestCase 
    DEF the tearDown (Self): 
        # operation done after each test case execution 
        Print ( ' 111 ' ) 

    DEF the setUp (Self): 
        # done prior to performing each test case operation 
        Print ( ' 22222 ' ) 

    @classmethod 
    DEF tearDownClass (Self): 
    # @ classmethod must use a decorator, once all the test run after run 
         Print ( ' 4444444 ' ) 
    @classmethod 
    DEF setUpClass (Self): 
    # You must use @classmethod decoration , run a test run before all the 
        print (' 33333 ' ) 

    DEF test_a_run (Self): 
        self.assertEqual ( . 1 , . 1 ) # test 
        
    DEF test_b_run (Self): 
        self.assertEqual ( 2 , 2 ) # test 
        
IF the __name__ == ' __main__ ' : 
    unittest.main ( ) # run all the test cases

unittest test frame loaded by default according to the order of ASCII code, as the order of numbers and letters: 0-9, AZ, az. Therefore TestAdd class will be better than TestBdd is performed, test_aaa () method will be better test_ccc executed, so it is not performed in order from top to bottom with the embodiment


If the function contains the function to be tested will connect to the database, or initiate http request. You do not want to start actual operation, or if you simply is not calling this function. Well, this time it is used in a mock module.

 ========== common assertion

assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b

assertIsInstance(obj,cls,msg=None)

assertNotIsInstance(obj,cls,msg=None)

==============

Installation HTMLTestRunner, execute test cases using test will generate a html report, the results of which will have to perform each test case

HTMLTestRunner Import         
        Import the unittest 
        class the MyTest (of unittest.TestCase): # Inheritance of unittest.TestCase 
            DEF the tearDown (Self): 
                # operation done after each test case execution 
                Print ( ' 111 ' ) 
            DEF the setUp (Self): 
                # perform each test case do before the operation 
                Print ( 22222 ) 
            DEF test_run (Self): 
                # self.assertEqual ( 1 , 1 ) 
                self.assertIs ( 1 , 1 ) 
                # test 
            def test_run2 (self):
                Self.assertEqual # ( 1 ,1 ) 
                self.assertIs ( 1 , 1 ) 
                # test 
            DEF test_run3 (Self): 
                # self.assertEqual ( 1 , 1 ) 
                self.assertIs ( 1 , 1 ) 
                # test 
            DEF test_run1 (Self): 
                # self.assertEqual ( . 1 , . 1 ) 
                self.assertIs ( . 1 , . 1 ) 
                # test 
        IF the __name__ == ' __main__ ' : 
            a test_suite = unittest.TestSuite () to create a test set # 
            test_suite.addTest (the MyTest ( ' test_run1 ' )) add a test case suite Test # 
            # test_suite.addTest (unittest.makeSuite (MyTest)) # makeSuite method using add all test method 
            FP = open ( ' res.html ' , ' WB ' ) # save the results to open a html file 
            Runner = HTMLTestRunner .HTMLTestRunner (Stream = FP, title = ' API test report ' , Description = ' test situation ' ) 
            Example # object with the generated execution
            runner.run (test_suite) 
            # test suite execution

 ============== find all test cases under the current directory

unittest Import, HTMLTestRunner 
        Suite = unittest.TestSuite () # create test suites 
        all_cases = unittest.defaultTestLoader.discover ( ' . ' , ' the Test _ *. Py ' ) 
        # Find all beginning to test Python files under a directory inside test case 
        for  Case  in all_cases: 
            suite.addTests ( Case ) was added to all test cases # incoming 
        FP = Open ( ' res.html ' , ' WB ' ) 
        Runner = HTMLTestRunner.HTMLTestRunner (Stream = FP, title = ' all_tests ', Description = ' all test cases ' ) 
        runner.run (Suite) 
        # Test Run

 discover () method start_dir can only load .py files in the current directory and subdirectories if the load .py files, _init_.py need to place a file in each subdirectory.

  • discover(start_dir,pattern='test*.py',top_level_dir=None)
  • start_dir: To test the module name or test cases directory
  • = pattern 'Test .py': matching principle embodiment is represented by the file name, ".py" file type in the file name here "test" the beginning, " " represents any number of characters.
  • top_level_dir = None: the top-level directory of the test module, if not the top-level directory, defaults to None.

===================

 

Guess you like

Origin www.cnblogs.com/testzcy/p/11220953.html