1: Unit test, integration test, function test
unit testing
The particle size is the smallest. Generally, the development team uses the white box method to test whether the main test units meet the requirements
"
Design
"
; It refers to the minimum allowable cost in the software
Check and verify the test unit
integration testing
Between unit test and system test, the development team generally adopts white box
+
Black box method to test, that is, verification
"
Design
"
Verify again
"
need
seek
"
. It is mainly used to test the interface between templates, as well as some main business functions.
functional testing
The particle size is the largest, which is generally tested by an independent test team in the form of black box. The main test system is whether it meets the "requirements specification"
Question: what is a white box test and what is a black box test
White box: it is mainly used in the unit test stage, mainly for the test of code level, aiming at the internal logical structure of the program. The means of testing include: statement
Coverage, decision coverage, condition coverage, path coverage and condition combination coverage
Black box: regardless of the internal structure and logical structure of the program, it is mainly to test whether the function of the system is met
"
Requirements specification
"
. There is usually one
The input value and an output value are compared with the expected value
2: Important components of Unittest
Python
There is a built-in unit testing framework in
unittest
Module, which is used for unit testing, and some verifications are encapsulated in it
The returned result method (assertion) and some use cases are initialized before execution.
unittest
The core part of the is:
TestFixture
,
TestCase
,
TestSuite
,
TestRunner
TestFixture
effect:
A test environment for preparing and restoring.
Function:
The test environment needs to be prepared before each execution of the test case, and the test environment needs to be restored after each test, such as connecting to the database and typing before execution
Open the browser, etc. after execution, you need to restore the database, close the browser, etc. It can be enabled at this time
testfixture
Main methods:
setUp()
: prepare the environment and execute the preconditions of each test case;
tearDown()
: restore the environment and execute the post conditions of each test case;
setUpClass()
: must be used
@classmethod
Decorator, all
case
Preconditions for execution, only run once;
tearDownClass()
: must be used
@classmethod
Decorator, all
case
Only run once after running;
TestCase: test case
definition
A class inherits unittest Testcase is a test case
What are test cases?
It is a complete test process, including the construction of the pre test preparation environment
(setUp)
, execute the test code
(run)
, and test the rear ring
Restoration of environment
(tearDown)
Test case naming rules
Inherited from
unittest.TestCase
In the class of, the name of the test method should be
test
start. And will only execute
test
Defined at the beginning
Method (test method)
,
The test cases will be executed in the order of method names
ASCII
Value sorting.
If you want to skip a test case, you need to add
@unittest.skip)('
Description information
')

Output result:
Implementation code:
import unittest class UnitTestClass(unittest.TestCase): def setUp(self) -> None: #Actions before test case method execution print("Preconditions of test cases") @classmethod #The @ classmethod decorator must be used def setUpClass(cls) -> None: print("All case Preconditions for execution, run only once") def test001(self): #test case print("Test case 1") resl = 4 self.assertEqual(resl,4) def test002(self): #test case print("Test case 2") resl = 6 self.assertEqual(resl, 6) def tearDown(self) -> None: # Operation after test case method execution print("Post condition of test case") @classmethod #The @ classmethod decorator must be used def tearDownClass(cls) -> None: print("All case Only run once after running") if __name__=="__main__": unittest.main() #Call the method starting with test in the test case
@unittest.skip skip a test case
Output result:
Implementation code:
import unittest class UnitTestClass(unittest.TestCase): def setUp(self) -> None: #Actions before test case method execution print("Preconditions of test cases") # @classmethod #The @ classmethod decorator must be used # def setUpClass(cls) -> None: # print("preconditions for all case execution, only run once") def test001(self): #test case print("Test case 1") resl = 4 self.assertEqual(resl,4) @unittest.skip("test002") #Skip a test case def test002(self): #test case print("Test case 2") resl = 6 self.assertEqual(resl, 6) def tearDown(self) -> None: # Operation after test case method execution print("Post condition of test case") # @classmethod #The @ classmethod decorator must be used # def tearDownClass(cls) -> None: # print("only run once after all case s are run") if __name__=="__main__": unittest.main() #Call the method starting with test in the test case
TestSuite
Test suite can gather multiple test cases together and execute the selected test cases together
Mode 1:
Output result:
Implementation code:
import unittest class UnitTestClass(unittest.TestCase): def test001(self): #test case print("Test case 1") resl = 4 self.assertEqual(resl,4) def test002(self): #test case print("Test case 2") resl = 6 self.assertEqual(resl, 6) if __name__=="__main__": unittest.main() #Call the method starting with test in the test case suite = unittest.TestSuite() ##Create test suite case_list = ["test001","test002"] for case in case_list: suite.addTest(UnitTestClass(case))
Mode 2:
Output result:
Implementation code:
import unittest class UnitTestClass(unittest.TestCase): def test001(self): #test case print("Test case 1") resl = 4 self.assertEqual(resl,4) def test002(self): #test case print("Test case 2") resl = 6 self.assertEqual(resl, 6) if __name__=="__main__": unittest.main() #Call the method starting with test in the test case suite = unittest.TestSuite() #Create test suite suite.addTest(UnitTestClass("test001")) suite.addTest(UnitTestClass("test002")) runner = unittest.TextTestRunner(verbosity=2) #verbosity: indicates the detail level of test report information. There are three values in total. The default value is 2 runner.run(suite)
TextRunner
Execute test cases
adopt
TextTestRunner
Class
run()
Method
verbosity
: indicates the detail level of test report information. There are three values in total. The default value is
2
0 (
silent mode
)
Total number of test cases you can only get, such as: total number of test cases
100
Failures
10
success
90
1 (
Default mode
)
: it is similar to the silent mode, but there is one in front of each successful use case
.
Each failed use case is preceded by a
F
2 (
Detailed mode
)
: the test results will display all relevant information of each test case
Mode 3
:

Output result:
Implementation code:
import unittest class UnitTestClass(unittest.TestCase): def test001(self): #test case print("Test case 1") resl = 4 self.assertEqual(resl,4) def test002(self): #test case print("Test case 2") resl = 6 self.assertEqual(resl, 6) if __name__=="__main__": unittest.main() #Call the method starting with test in the test case suite = unittest.TestSuite()#Create test suite loader = unittest.TestLoader()# Create a load object suite.addTest(loader.loadTestsFromTestCase(UnitTestClass))