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, is understood as a collection of test cases and a collection of test suites
  • When running a test suite, all test cases added in it are run

test runner

  • test runner
  • Components for executing and outputting results

Basic use of test suite and test runner

Unit test class

 1 # Create a unit test class and inherit unittest.TestCase
 2 class testCase(unittest.TestCase):
 3
 4 # Test 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 # Instantiate test suite
 3 suite = unittest.TestSuite()
 4 # Instantiate the second test suite
 5 suite1 = unittest.TestSuite()
 6 # Add test cases - Method 1
 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 # Add test cases - Method 2
12 testcase = (testCase('test_05'), testCase('test_04'))
13 suite.addTests(testcase)
14 # Test suite Add test suite
15 suite.addTest(suite1)
16 # Instantiate the TextTestRunner class
17 runner = unittest.TextTestRunner()
18 # Run test suite
19 runner.run(suite)

Run results

 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 they are added.
  • There are two ways to add test cases. The recommended method is the second way, which has less code and is faster.
  • , the incoming tests can be list, tuple, set

addTests(tests)

  • The added test case format is:

Unit test class name (test case name)

  • The general steps for executing test cases using test suites are: instantiate TestSuite – add test cases – instantiate TextTestRunner – run the test suite
  • Test suites can also add test suites
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036 [password: csdn999]

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 and does not contain test cases.

List the code of a certain unit test class file

 1 # Create a unit test class and inherit unittest.TestCase
 2 class testCase02(unittest.TestCase):
 3
 4 # Test 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 of running 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 # Pass module
 7 testcase02 = unittest.TestLoader().loadTestsFromModule(test_case02)
 8 # Pass unit test class
 9 testcase03 = unittest.TestLoader().loadTestsFromTestCase(testCase03)
10 # Pass module string
11 testcase04 = unittest.TestLoader().loadTestsFromName('learn.unittestLearning.test_case04')
12 # Test case set
13 tests = [testcase02, testcase03, testcase04]
14 # Create test suite
15 suite = unittest.TestSuite(tests)
16 # Run test suite
17 unittest.TextTestRunner(verbosity=2).run(suite)

Contains knowledge points

  • :testCaseClass enters the unit test class, but needs to be imported first

loadTestsFromTestCase(testCaseClass)

  • :module enters the module where the unit test class is located, and also needs to be imported.

loadTestsFromModule(module, pattern=None)

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

loadTestsFromName(name, module=None)

  • : Indicates the information details of the test results. There are three values in total. The default is 1.
    • 0 (silent mode): You can only get the total number of test cases and the total results, for example, a total of 100, 20 failures, 80 successes
    • 1 (default mode): Very similar to silent mode, except there is an F before each successful use case and an F before each failed use case.
    • 2 (Detailed mode): The 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 # Directory of unit test files that need to be run
5 test_path = './'
6 # Instantiate defaultTestLoader
7 discover = unittest.defaultTestLoader.discover(start_dir=test_path, pattern="test_case*.py")
8 #Run the test case set
9 unittest.TextTestRunner().run(discover)

Advantages: It is simple. . Is it fast? ? Just three lines of code! !

Contains knowledge points

  • :Write the directory of unit test files that need to be run

start_dir

  • : Matching rules for unit test files. 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 based on the test directory start_dir match, and assemble the found test cases into the test suite, so it can be executed directly through the run() method discover

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

END Today’s sharing ends here, please like and follow to avoid getting lost~