--unit test
The granularity is the smallest, and it is generally tested by the development team using a white box method, mainly to test whether the unit meets the "design"; refers to
Check and verify the smallest testable unit in the software
Integration Testing
Between unit testing and system testing, the development team generally uses the white box + black box method to test, that is, the test
Verify the "design" and verify the "requirements". It is mainly used to test the interface between the template and the template, and also to test some main
business functions.
function test
The granularity is the largest, and it is generally tested by an independent test team using a black box method, mainly to test whether the system meets the "requirements
specification sheet
What is white box testing and what is black box testing?
White box:
It is mainly used in the unit testing stage, mainly for code-level testing, and for the internal logic structure of the program. test
The methods are: 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 meets the "requirement specification". one
Generally, there will be an input value and an output value, which are compared with the expected value.
Important components of Unittest
There is a built-in unit test framework in Python is the unittest module, which is used for unit testing, and it is packaged inside
It introduces some verification methods (assertions) for returning results and some initialization operations before execution of use cases.
The core parts of unittest are: TestFixture, TestCase, TestSuite, TestRunner
Test Fixture:
effect:
Prepare and restore for a test environment
Function:
When the test case needs to prepare the test environment before each execution, and restore the test environment after each test, such as executing
Before running, connect to the database, open the browser, etc. After the execution is completed, you need to restore the database, close the browser, etc. At this moment
You can enable testfixture
Main method:
setUp(): prepare the environment, execute the preconditions of each test case;
tearDown(): restore the environment, execute the postconditions of each test case;
setUpClass(): must use the @classmethod decorator, before executing all cases Set the condition and only run once;
tearDownClass(): must use the @classmethod decorator, and only run once after all cases are run;
TestCase: test case
definition:
A class class inherits unittest.TestCase, which is a test case
What is a test case:
It is a complete test process, including setting up the preparation environment before the test (setUp), executing the test code (run),
And the restoration of the environment after the test (tearDown).
Test case naming rules:
In classes inherited from unittest.TestCase, the name of the test method must start with test. and will only be executed with test
For the method (test method) defined at the beginning, the order of test case execution 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')
main:
You will only use the main method when the current module is executed. If it is called in other modules, it will not be executed.
It is executed according to ASCLL
does not start with test
If it does not start with test, test1 will not be found
Create test suite
Affirmation
Verify expected and actual results:
assertEqual(a,b): Asserts whether a and b are equal, and the test case passes if they are equal.
assertNotEqual(a,b): Asserts whether a and b are equal, if not equal, the test case passes.
assertTrue(x): Assert whether x is True, if it is True, the test case passes.
assertFalse(x): assert 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.
assertNotIs(a,b): assert whether a is b, if not, 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.
assertIn(a,b): Asserts whether a is in b, and the test case passes in b.
assertNotIn(a,b): assert whether a is in b, if not in b, the test case passes.
assertIsInstance(a,b): assert that a is an instance of b, and if so, the test case passes.
assertNotIsInstance(a,b): assert that a is an instance of b, if not, the test case passes.
Such as: assertEqual(a,b)
Generate test report
HTML format is HTMLTestRunner, HTMLTestRunner is the unittest framework of Python standard library
An extension of , which can generate an intuitive and clear HTML test report. The prerequisite for use is to download HTMLTestRunner.py
Format:
with open("../report.html", "wb") as f:
HTMLTestRunner(
stream=f,
title="Unit Test",
description="Test Phase 1",
verbosity=2
).run(suite)
Related parameter description:
stream: Specifies the output method
description: The familiar information to be displayed in the report
title: The title of the test report
verbosity: Indicates the detail level of the test report information, a total of three values, the default is 2
0 (silent mode): you can only get the total The number of test cases and the total results, such as: a total of 100 failures 10 successes 90
1 (default mode): similar to silent mode, but there is an F
2 ( Verbose mode): Test results will display all relevant information for each test case
Copy HTMLTestRunner.py into it
After running:
There is one more html file on the left
Double-click to open, the effect cannot be seen Click the browser in the upper right corner
as shown in the picture
If there is an error:
After clicking file
Six, code display
Develop the code under test
Unit test code:
read file
read xml file
xml introduction
What is xml:
XML stands for Extensible Markup Language
XML is a markup statement, much like HTML
XML tags are not predefined, you need to define tags yourself
The difference between xml and html:
1. XML is designed for different purposes
2.XML is designed to transmit and store data, its focus is on the content of the data, HTML is designed to display data,
Its focus is on how the data looks
3.XML is intended to transmit information, while HTML is intended to display information
xml features:
1.xml can customize tags
2.xml must contain a root element
3.xml tags are case sensitive
4. The attribute value of xml must be quoted
xml comments:
<!-- content -->
What is an xml element:
The value of an xml element is from (and including) the start tag up to (and including) the end tag
Elements can contain other elements, text, or a mixture of both. Elements can also have attributes
xml naming rules:
Names can contain letters, numbers, and other characters
Name cannot begin with a number or punctuation
Name cannot begin with the characters "xml" (or XML, Xml)
Name cannot contain spaces
code:
correct:
Error after modifying the value: