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 comply with the "design"; It refers to the inspection and verification of the smallest testable unit in the software
integration testing
Between unit test and system test, the development team generally adopts the method of white box + black box to test, that is, to verify "design" and "requirements". 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. Generally, the independent test team uses the black box method to test whether the main test system meets the "requirements specification"
What is a white box test and what is a black box test
White box: it is mainly used in the unit test stage. It is mainly used to test the code level and the internal logical structure of the program. The test methods 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 mainly tests whether the function of the system meets the "requirements specification". Generally, there will be an input value and an output value, which will be compared with the expected value.
2: Important components of Unittest
Python has a built-in unit test framework, unittest module, which is used for unit testing. It encapsulates some result methods (assertions) returned by verification and some initialization operations before use case execution.
The core parts of unittest are: TestFixture, TestCase, TestSuite and TestRunner
TestFixture
effect:
It is used for the preparation, destruction and restoration of a test environment.
Function:
When the test case needs to prepare the test environment before each execution, restore the test environment after each test, such as connecting to the database and opening the browser before execution, and restore the database and close the browser after execution. At this time, you can enable testfixture
Main methods:
setUp(): prepare the environment and execute the preconditions of each test case;
tearDown(): environment restore, execute the post condition of each test case;
setUpClass(): the @ classmethod decorator must be used. The preconditions executed by all case s can only be run once;
tearDownClass(): the @ classmethod decorator must be used. All case s can only be run once after running;
TestCase: test case
definition
A class class inherits unittest.TestCase, which is a test case
What are test cases?
It is a complete testing process, including setting up the environment before testing, executing the test code (run), and restoring the environment after testing (tearDown).
Test case naming rules
In the class inherited from unittest.TestCase, the name of the test method should start with test. Only the methods (test methods) defined beginning with test will be executed, and the execution order of the test cases will be sorted according to the ASCII value of the method name.
If you want to skip a test case, you need to add @ unittest. Skip ('description information ')
import unittest class Calc_testcase(unittest.TestCase): def setUp(self) : #Actions before test case method execution print("start") def test1(self): #test case resl = 4 self.assertEqual(resl,5) def tearDown(self) : #Actions after test case method execution print("end") if __name__ =="__main__": unittest.main() #Call the method starting with test in the test case
TestSuite
Test suite, which can collect multiple test cases together and execute the selected test cases together
Mode 1:
suite = unittest.TestSuite()#Create test suite case_list = ["test1","test2"....] For case in case_list: suite.addTest(Class name(case))
Mode 2:
suite = unittest.TestSuite()#Create test suite suite.addTest(Class name ("test1")) suite.addTest(Class name ("test2"))
Mode 3:
suite = unittest.TestSuite()#Create test suite loader = unittest.TestLoader()# Create a load object suite .addTest(loader.loadTestsFromTestCase(Class name))
TextRunner
Execute test cases
Execute test suite/test cas through the run() method provided by the TextTestRunner class
format
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
notes
verbosity: indicates the detail level of test report information. There are three values in total. The default value is 2
3: Assert
Verify expected and actual results
assertEqual(a,b):Assert a and b The test case passes if it is equal. assertNotEqual(a,b):Assert a and b Whether it is equal or not. If it is not equal, the test case passes. assertTrue(x): Assert x whether True,yes True The test case passes. assertFalse(x): Assert x whether False,yes False The test case passes. assertIs(a,b):Assert a Is it b,Yes, the test case passes. assertNotIs(a,b):Assert a Is it b,If not, the test case passes. assertIsNone(x): Assert x whether None,yes None The test case passes. assertIsNotNone(x): Assert x whether None,no None The test case passes. assertIn(a,b): Assert a Is it b In, in b The test case passes. assertNotIn(a,b): Assert a Is it b In, not in b The test case passes. assertIsInstance(a,b): Assert a Yes, yes b An example of is that the test case passes. assertNotIsInstance(a,b): Assert a Yes, yes b If not, the test case passes.
4: Generate test report
The HTML format is HTMLTestRunner. HTMLTestRunner is an extension of the unittest framework of the Python standard library. It can generate an intuitive and clear HTML test report. The premise of use is to download HTMLTestRunner.py
format
with open("../report.html","wb") as f: HTMLTestRunner( stream=f, title="unit testing ", description="Test phase I", verbosity=2 ).run(suite)
Generate test report
stream: Specify how to output description: Familiar information to be displayed in the report title: Test report title verbosity : Indicates the detail level of test report information. There are three values in total. The default value is 2 0 (silent mode ): You can only get the total number of test cases and total results, such as 100 failures, 10 successes and 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 for each test case
5: Operation
1: Import unittest modular >>>import unittest 2:Write a class inheritance unittest.TestCase 3:call setUp(self), tearDown(self)Method to realize the operation before and after the test case 4:Method of writing test cases (1)The method must be test start,Otherwise in unittest.main()This method cannot be found in calling tests. (2)Set assertions to judge the expected results of input data and output data 5:Create a suite, store multiple test cases in the suite and execute them together() 6:Generate test report(python Bring or import HTMLTestRunner generate html Test report in format) 7: Run test cases unittest.main(),Call in the test case to test Method at the beginning
6: Code display
Unit test the developed code block
Develop the tested code
class Calc(): def add(self,a,b): c =a+b return c def redc(self,a,b): c = a-b print(c) if __name__ == "__main__": c = Calc() c.add(2,3) print(c.add(2,3))
Unit test code
from day.Calc import Calc import unittest c = Calc() #Instantiate developed classes class Test(unittest.TestCase):#Unittest unit test class must inherit unittest.TestCase def setUp(self): #Execute before test case print("start") def test001(self):#The test case must start with test res = c.add(2,1) #Method of calling addition self.assertEqual (res,5) #Assertion, comparison between expected results and actual results def test002(self):#The test case must start with test res = c.redc(2,3) #Method of calling subtraction self.assertEqual (res,-1) #Assertion, comparison between expected results and actual results def tearDown(self):#Execute after the test case is completed print("end") if __name__ == '__main__': unittest.main()
7: Read file
Read xml file
from xml.dom import minidom class Readxml(): def read_xml(self,filename,onename,twoname): root =minidom.parse(filename) firstnode =root.getElementsByTagName(onename)[0] secondnode=firstnode.getElementsByTagName(twoname)[0].firstChild.data return secondnode
Read csv file
import csv #Import csv module class ReadCsv(): def read_csv(self): item =[] #Define an empty list c = csv.reader(open("../commonDemo/test1.csv","r")) #Get csv file object for csv_i in c: item.append(csv_i) #Add the acquired data to the list return item r = ReadCsv() print(r.read_csv())