Python unittest discover () method and the execution sequence

A, discover more test cases

You can create different functions according to different test files, directories or even a different test, test files can also be different small test functions are divided into different classes, write test cases in class, so that the overall structure is more clear

But () by adding addTest, delete a test case becomes very troublesome

TestLoader class provided Discover () method can automatically identify test

discover(start_dir,pattern='test*.py',top_level_dir= None)

Find all test modules specified directory, and can be found in the test module in the recursive subdirectory only match to the file name when load

start_dir: To test the module name or a test case directory

pattern = 'test * .py': indicates the file name to match with the principles of embodiments. Here .py matches the type of file "test" beginning, * represents any number of characters

top_level_dir = None of the top-level directory of the test module, if not the top-level directory, defaults to None

Example 1:

. 1  Import the unittest
 2 test_dir = ' ./ ' 
. 3  # defined test directory as the current directory 
. 4 Discover = unittest.defaultTestLoader.discover (test_dir, pattern = ' Test * .py ' )
 . 5  
. 6  IF  the __name__ == ' __main__ ' :
 . 7      Runner = unittest.TextTestRunner ()
 . 8      runner.run (Discover)
 . 9  Discover () method will automatically find the test case file according to the test directory test_dir match and find the test suite to test assembled, therefore, directly through
 10  RUN () method performs discover, greatly simplify the search and execute test cases
 . 11  
12 is  example 2:
 13 is= Suite unittest.TestSuite ()
 14 all_cases = unittest.defaultTestLoader.discover (PY_PATH, ' the Test * .py ' )
 15  # Discover () according to the test method of automatically match lookup directory file test (Test * .py), and to find the test suite to test assembly 
16 [suite.addTests (Case) for Case in all_cases]
 . 17 report_html = BeautifulReport.BeautifulReport (Suite)
Second, performed sequentially with Example 
unittest framework default load test in the order of ASCII code, the order of numbers and letters: 0 ~ 9, A ~ Z , a ~ z
If you want to execute a test case, not using the default main () method, requires () method in a certain order to load by addTest TestSuite class

source: https: //blog.csdn.net/happyuu/article/details/80683161

Guess you like

Origin www.cnblogs.com/liuyanhang/p/11121013.html