Python uses the Discover method to traverse all the use cases to be executed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.