Objective
When we write a use case, the use case of a single script executes well, so how do you batch execute multiple scripts? This is the time to use the Discover method inside the Unittet to load the use case. After loading the use case, use the Run method of the Texttestrunner class inside the UnitTest to execute multiple scripts at once.
First, the new test project
1.pycharm upper left corner file>new projetc>pure Python, name a test project in location: Mytest, then save;
2. Right-click on the newly created project >new>python package> to create a new unitest Python package, note that unitest, not unittest
3. Right-click the Unitest package created in step 2 to create a case package
4. Repeat step 3, under Case package, create the Baidu and Wangyi packages in turn
5. Create 2 test Case scripts below the Baidu and Wangyi packages, TESTA,TESTB,TESTC,TESTD is our script for writing use cases.
After you create the hierarchy, such as:
After the 6.testA script is created, open the file, write the use case, and the contents of the other three test cases are similar
The script content is:
1 # coding:utf-8 2 3 import unittest 4 5 # Print Help (unitest) 6 class TestA (unittest. TestCase): 7 8 def setup (self): 9 print "TestA------Setup" pass11 def TearDown ( Self): print "TestA------tearDown", pass15 def test_a001 (self): print ("test_a001 ") def test_a002 (self): print (" test_a002 ")
7. Create a script run_all_case.py below unitest, then use this script to execute all the use cases in bulk.
Second, diascover load test Cases
There are three parameters in the 1.discover method:
-case_dir: This is the directory where the use case is to be executed.
-pattern: This is the rule that matches the script name, test*.py means all the scripts that match the beginning of the test.
-top_level_dir: This is the name of the top-level directory, the general default is equal to none on the line.
The use case that 2.discover loads into is a list collection that needs to be re-written to a list object testcase, so that it can be executed using the Run method of the Texttestrunner class here in UnitTest.
3. The results of the operation are as follows:
1 C:\Python27\python.exe c:/users/zhangxiaoding/pycharmprojects/mytest/unitest/Run_all_case.py2C:\Users\zhangxiaoding\PycharmProjects\Mytest\unitest\report3 C:\Users\zhangxiaoding\PycharmProjects\Mytest\unitest\report4 <unittest.suite.testsuite tests=[< Unittest.suite.TestSuite Tests=[<unittest.suite.testsuite Tests=[<case.baidu.testa.testa Testmethod=test_ A001>, <case.baidu.testa.testa testmethod=test_a002>]>]>, <unittest.suite.testsuite tests=[< Unittest.suite.TestSuite tests=[<case.baidu.testb.testb Testmethod=test_b001>, <case.baidu.testb.testb Testmethod=test_b002>]>]>, <unittest.suite.testsuite tests=[<unittest.suite.testsuite tests=[< Case.wangyi.testC.testC testmethod=test_c001>, <CASE.WANGYI.TESTC.TESTC Testmethod=test_c002>]>]>, <unittest.suite.testsuite Tests=[<unittest.suite.testsuite tests=[<case.wangyi.testd.testd testMethod= Test_d001>, <case.wangyi.testd.testd testmethod=test_d002>]>]>]>5 ..... 6 Time elapsed:0:00:00.0390007 8 Process finished with exit code 0
All of the test cases where discover is loaded are as follows:
1 <unittest.suite.testsuite tests=[<unittest.suite.testsuite Tests=[<unittest.suite.testsuite tests=[< Case.baidu.testA.testA testmethod=test_a001>, <case.baidu.testa.testa Testmethod=test_a002>]>]>, <unittest.suite.testsuite Tests=[<unittest.suite.testsuite Tests=[<case.baidu.testb.testb testMethod= Test_b001>, <case.baidu.testb.testb testmethod=test_b002>]>]>, <unittest.suite.testsuite tests=[ <unittest.suite.testsuite TESTS=[<CASE.WANGYI.TESTC.TESTC Testmethod=test_c001>, < Case.wangyi.testC.testC testmethod=test_c002>]>]>, <unittest.suite.testsuite tests=[< Unittest.suite.TestSuite tests=[<case.wangyi.testd.testd Testmethod=test_d001>, <case.wangyi.testd.testd Testmethod=test_d002>]>]>]>
4. After running the result, a report folder is created under Unitest (not created if no folder is created), which contains the test reports after the end of the run.
5. Open the test report, as
Python Unit Test Framework unittest batch use case management (Discover)