Introduction to the

When we write use cases, the use cases of a single script are easy to execute, so how to execute multiple scripts in batches? At this point, use the Discover method in UnitTest to load the use case. After loading the use case, use TextTestRunner in UnitTest where the class’s Run method executes multiple legs at once

Ben’s use case. So the previous introduction of so many are half way, half way monk, this article will take you from beginning to end, step by step to give a detailed introduction to your friends.

Creating a Test Project

Pycharm top left File>New Projetc>Pure Python, name a test project in the location: honggeTest

2. Then click “Create”

3, Right-click the New project >New>Python Package> create a case folder

4. Repeat step 3 to create a new case folder and add baidu and Blog folders with two use case scripts respectively, as shown in the figure below. Test_01,test_02,test_03, and test_04 are the scripts we use to write our use cases

5. After test_01 is created, open the script and write the use case. The rest can be copied

6. Reference code

 1 # coding:utf-8
 2 import unittest
 3 import time
 4 
 5 class Test(unittest.TestCase):
 6     def setUp(self):
 7         print ("start!")
 8     def tearDown(self):
 9         time.sleep(1)
10         print ("end!")
11     def test01(self):
12         print ("Execute test Case 01")
13     def test02(self):
14         print ("Execute test Case 02")
15     def test03(self):
16         print ("Execute Test Case 03"17 18)if __name__ == "__main__":
19     unittest.main()Copy the code

7. Create a script run_all_case.py under the honggetest project and use this script to batch execute all use cases.

Discover loads test cases

1. Discover has three parameters:

-case_dir: This is the directory of the use case to be executed.

-pattern: This is the rule that matches the names of scripts. Test *. Py means to match all scripts starting with test.

-top_level_dir: This is the name of the top level directory, which defaults to None.

2. Discover is a list set that needs to be rewritten into testCase, a list object, so that TextTestRunner in UnitTest can be executed using the run method of the class.

3. After the run, the result is entered below, which is all the test cases loaded:

Reference code

 1 # coding=utf-8
 2 #1. Set the encoding first, utF-8 can support Both Chinese and English, as above, usually in the first line3, 4,#2. Comments: include record creation time, creator, project name.
 5 ' ''6 Created on 2019-4-29 7 @author: discover (Discover, Discover, Discover)' '
10 Import unitTest module
11 import unittest
12 #4. Write test cases and assertions
13 def all_case():
14     The directory of use cases to execute
15     case_dir = "C:\\Users\\DELL\\PycharmProjects\\honggetest\\case"
16     testcase = unittest.TestSuite()
17     discover = unittest.defaultTestLoader.discover(case_dir,
18                                                    pattern="test*.py",
19                                                    top_level_dir=None)
20     The # Discover method filters out use cases and adds them in a loop to the test suite
21     for test_suit in discover:
22         for test_case in test_suit:
23             # Add force to testCase
24             testcase.addTests(test_case)
25     print(testcase)
26     return testcase
27 if __name__ == "__main__"28:# return instance
29     runner = unittest.TextTestRunner()
30     # run all use cases
31     runner.run(all_case())Copy the code

Run test case

1. For a more convenient understanding, the discover loading method above can be encapsulated as a function

2. Return an instance of the TextTestRunner() class

3. Call the run method to execute all_case()

Reference code

# coding=utf-8
#1. Set the encoding first, utF-8 can support Both Chinese and English, as above, usually in the first line

#2. Comments: include record creation time, creator, project name.
' ''How to Batch Execute Test Cases with Discover'' '
Import unitTest module
import unittest
#4. Write test cases and assertions
def all_case():
    The directory of use cases to execute
    case_dir = "C:\\Users\\DELL\\PycharmProjects\\honggetest\\case"
    testcase = unittest.TestSuite()
    discover = unittest.defaultTestLoader.discover(case_dir,
                                                   pattern="test*.py",
                                                   top_level_dir=None)
    testcase.addTests(discover)  Discover is compatible with python2 and 3
    print(testcase)
    return testcase
if __name__ == "__main__":
    # return instance
    runner = unittest.TextTestRunner()
    # run all use cases
    runner.run(all_case())Copy the code

summary

1.

The TestLoader class in the UnitTest module has a Discover method (after Python2.7)
discover(

s

tart_dir
pattern=’test*.py’
top_level_dir=None)

Back. Only test files that match pattern will be loaded into TestSuite. If a test file’s name matches pattern, the file is checked for load_tests(). If load_tests() exists, the load_tests() function is responsible for loading the test cases in the file. If not, it will be executed

LoadTestsFromModule (), which looks for methods starting with test contained in the class derived from TestCase in this file.

2. Another way to write the use-case path
1 case_dir = os.path.join(os.getcwd(), "case") In order to facilitate you to see my blog posts on the mobile terminal, I have registered my personal wechat public account and can scan the QR code at the bottom left. You are welcome to pay attention to it. I will share relevant technical blog posts in time. In order to facilitate the interaction and discussion of relevant technical issues, a special wechat group has been set up. Since the number of wechat groups is 100, please scan the qr code of Hongge's personal wechat to draw you into the group (please note: the public account has been followed to enter the group). Welcome to join this big family, and we will enjoy the ocean of knowledge together. Thank you for taking the time to read this article. If you think you have learned something from this article, it is also for rewarding the blogger for having a cup of coffee. Thank you! If you think reading this article is helpful to you, please click the "recommendation" button in the lower left corner, your "recommendation" will be my biggest motivation to write! In addition, you can also choose [focus on me], it is very convenient to find me! Copyright belongs to the author in this paper, and blogs, gardens of source url: https://www.cnblogs.com/du-hong welcome to reprint, but without the permission of the author himself, reprint articles must be clearly position is given in the article page after the author and the original connection, or reserve the right to shall be investigated for legal responsibility!Copy the code