Python interface automated testing - detailed use of unittest framework suite and runner

test suite

  • Test suite, understood as a set of test cases
  • A series of test cases, or test suites, understood as a collection of test cases and a collection of test suites
  • When running the test suite, run all the test cases added inside

test runner

  • test runner
  • Components for execution and output of results

Basic use of test suite and test runner

unit test class

 1 # 创建单元测试类,继承unittest.TestCase
 2 class testCase(unittest.TestCase):
 3 
 4     # 测试case
 5     def test_01(self):
 6         print("test01")
 7 
 8     def test_03(self):
 9         print("test03")
10 
11     def test_04(self):
12         print("test04")
13 
14     def test_05(self):
15         print("test05")

main function

 1 if __name__ == '__main__':
 2     # 实例化测试套件
 3     suite = unittest.TestSuite()
 4     # 实例化第二个测试套件
 5     suite1 = unittest.TestSuite()
 6     # 添加测试用例 - 方式一
 7     suite.addTest(testCase('test_03'))
 8     suite.addTest(testCase('test_01'))
 9     suite1.addTest(testCase('test_03'))
10     suite1.addTest(testCase('test_01'))
11     # 添加测试用例 - 方式二
12     testcase = (testCase('test_05'), testCase('test_04'))
13     suite.addTests(testcase)
14     # 测试套件添加测试套件
15     suite.addTest(suite1)
16     # 实例化TextTestRunner类
17     runner = unittest.TextTestRunner()
18     # 运行测试套件
19     runner.run(suite)

operation result

 1 test03
 2 test01
 3 test05
 4 test04
 5 test03
 6 test01
 7 ......
 8 ----------------------------------------------------------------------
 9 Ran 6 tests in 0.000s
10 
11 OK

Contains knowledge points

  • When using a test suite, the execution order of test cases can be customized and executed in the order added
  • There are two ways to add test cases, the recommended way is the second, with fewer codes and faster
  • , the incoming tests can be list, tuple, set

addTests(tests)

  • The format of the added test case is:

Unit test class name (test case name)

  • The general steps to execute a test case using a test suite are: instantiate TestSuite - add a test case - instantiate TextTestRunner - run the test suite
  • Test suites can also add test suites
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

Batch execution of test cases

unit test class file

The first three files are files that contain unit test classes, and the fourth file is responsible for running all unit test classes, excluding test cases

List a unit test class file code

 1 # 创建单元测试类,继承unittest.TestCase
 2 class testCase02(unittest.TestCase):
 3 
 4     # 测试case
 5     def test_07(self):
 6         print("testCase02 test07")
 7 
 8     def test_06(self):
 9         print("testCase02 test06")
10 
11     def test_11(self):
12         print("testCase02 test11")

test_run.py code

Method 1 to run test cases in batches:

 1 import unittest
 2 from learn.unittestLearning import test_case02
 3 from learn.unittestLearning.test_case03 import testCase03
 4 
 5 if __name__ == '__main__':
 6     # 通过模块
 7     testcase02 = unittest.TestLoader().loadTestsFromModule(test_case02)
 8     # 通过单元测试类
 9     testcase03 = unittest.TestLoader().loadTestsFromTestCase(testCase03)
10     # 通过模块字符串
11     testcase04 = unittest.TestLoader().loadTestsFromName('learn.unittestLearning.test_case04')
12     # 测试用例集
13     tests = [testcase02, testcase03, testcase04]
14     # 创建测试套件
15     suite = unittest.TestSuite(tests)
16     # 运行测试套件
17     unittest.TextTestRunner(verbosity=2).run(suite)

Contains knowledge points

  • : testCaseClass input unit test class, but need to import first

loadTestsFromTestCase(testCaseClass)

  • :module Input the module where the unit test class is located, also need to import

loadTestsFromModule(module, pattern=None)

  • : name is a string, which must meet the following format: module.class.method, which can only be input to class

loadTestsFromName(name, module=None)

  • : Indicates the details of the test results, a total of three values, the default is 1
    • 0 (silent mode): You can only get the total number of test cases and the total results, such as a total of 100 failed 20 successful 80
    • 1 (default mode): very similar to silent mode except that each successful case is preceded by an F before each failed case
    • 2 (detailed mode): test results will display all relevant information for each test case

verbosity

Method 2 of running test cases in batches (recommended!!):

1 import unittest
2 
3 if __name__ == '__main__':
4     # 需要运行的单元测试文件目录
5     test_path = './'
6     # 实例化defaultTestLoader
7     discover = unittest.defaultTestLoader.discover(start_dir=test_path, pattern="test_case*.py")
8     # 运行测试用例集
9     unittest.TextTestRunner().run(discover)

Pros: Is it concise. . Is it fast? ? Just three lines of code! !

Contains knowledge points

  • : Write the unit test file directory that needs to be run

start_dir

  • : The matching rule of the unit test file, the default is test*.py, you can modify this rule according to your own naming rules

pattern

  • The method can automatically find the test case file test*.py according to the test directory start_dir match, and assemble the found test cases into the test suite, so the discover can be executed directly through the run() method

discover()

Results of batch execution of test cases

 1 testCase02 test06
 2 testCase02 test07
 3 testCase02 test11
 4 testCase03 test05
 5 testCase03 test08
 6 testCase03 test12
 7 testCase04 test02
 8 testCase04 test04
 9 testCase04 test13
10 .........
11 ----------------------------------------------------------------------
12 Ran 9 tests in 0.000s
13 
14 OK

The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/132240160
Recommended