1, Unit test, integration test, function test
1.1 unit test
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
1.2 integration test
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.
1.3 function test
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"
1.4 white box and black box test
White box: it is mainly used in the unit testing stage, mainly for code level testing, aiming at the internal logical structure of the program. The test means 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 to compare with the expected value.
2: Important components of Unittest
Python A built-in unit test framework is 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.
unittest The core part of the is: TestFixture,TestCase,TestSuite,TestRunner
TestFixture
effect:
It is used for the preparation, destruction and restoration of a test environment.
Function:
Before each test case execution, the test environment needs to be prepared. After each test, the test environment needs to be restored, such as connecting the database and opening the browser before execution. After execution, the database needs to be restored and the browser needs to be closed. 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 use@classmethod Decorator, all case Preconditions for execution, run only once; tearDownClass(): Must use@classmethod Decorator, all case Only run once after running;
TestCase: test case
definition
A class class inherit unittest.TestCase,Is a test case
What are test cases?
It is a complete test process, including the construction of pre test preparation environment(setUp),Execute test code(run),And restore the environment after testing(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 Method defined at the beginning (test method),Test cases are 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')
code
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
Test suite, which can collect multiple test cases together and execute the selected test cases together
Three methods
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 case
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
Assert equal (a, b): assert whether a and B are equal. If they are equal, the test case passes.
Assert not equal (a, b): assert whether a and B are equal. If not, the test case passes.
assertTrue(x): asserts whether x is True. If it is True, the test case passes.
assertFalse(x): asserts whether x is False. If it is False, the test case passes.
assertIs(a,b): assert whether a is B. If yes, the test case passes. Assert not is (a, b): assert whether a is B, otherwise the test case passes.
assertIsNone(x): assert whether x is None. If it is None, the test case passes.
assertIsNotNone(x): assert whether x is None. If it is not None, the test case passes.
Assert in (a, b): assert whether a is in B. if it is in B, the test case passes.
Assert not in (a, b): assert whether a is in B. if not, the test case passes.
assertIsInstance(a,b): assert that a is an instance of B, and if yes, the test case passes.
Assert not isinstance (a, b): assert that a is an instance of B, otherwise the test case passes.
4: Generate test report
html The format is HTMLTestRunner Well, HTMLTestRunner yes Python Standard library unittest An extension of the framework, which 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)
Description of relevant parameters
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 steps
1: Import unittest module > > > Import unittest
2: Write a class that inherits unittest TestCase
3: Call the setup (self) and teardown (self) methods to realize the operation before and after the test case
4: How to write test cases:
(1) The method must start with test, otherwise it is in unittest The method can not be found in main ().
(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 reports (test reports in html format are generated by python or imported into HTMLTestRunner)
7: Run the test case unittest Main(), call the method starting with test in the test case
6, 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())
task
1. Unit test: test the developed addition, subtraction, multiplication and division function to transfer two parameters**
2. Read data through csv file and xml file, conduct unit test and generate html
csv