When we write a single py test file when run directly on OK, but when we have a lot of a lot of such py
, does it have to be one click to run it, of course not. We can use the Discover method to find all the
Case.
The following is a direct illustration of discover usage:
First, the preparatory work
Directory structure:
discovercase.py File Code
import unittestimport osdef discover_case(case_dir): # 待执行用例的目录 testcase = unittest.TestSuite() discover = unittest.defaultTestLoader.discover(case_dir,pattern="*.py",top_level_dir=None) # discover方法筛选出来的用例,循环添加到测试套件中 print(discover) for test_suite in discover: for test_case in test_suite: print(test_case) # 添加用例到testcase #testcase.addTests(test_case) testcase.addTests(test_case) return(testcase)path = os.path.join(os.getcwd(), "测试用例")case = discover_case(case_dir=path)print(case)
Test1 Code (TEST2~4 code is basically the same):
Note: The execution case in each testcase (that is, the function that starts with test) must be greater than or equal to two, or it will be an error.
Second, after writing this, we'll run the program and see the results.
The file name, class name, and function name of the post-run use case will be traversed.
Isn't it simple?
Note: If the use case name is all Chinese and cannot be loaded, you must start with a letter, such as "I login. Py"
This can be loaded into the, if directly write "login. Py" This is not loaded into the.
Python uses the Discover method to traverse all the use cases to be executed