当前位置:网站首页>unit testing
unit testing
2022-07-04 10:48:00 【weixin_ thirty-four million three hundred and twenty-two thousa】
(1) Definition :
unit testing ( Also known as module testing ) It's for program modules ( The smallest unit of software design ) To test the correctness .
A program unit is the smallest testable part of an application . In procedural programming , A unit is a single program 、 function 、 Process, etc ; For object-oriented programming , The smallest unit is the method , Including base classes ( Superclass )、 abstract class 、 Or derived classes ( Subclass ) The method in .
(2) Unit test tasks include :
1 Module interface test ;
2 Module local data structure test ;
3 Module boundary condition test ;
4 All in the module independently perform path testing ;
5 Test each error handling path of the module .
Module interface testing is the foundation of unit testing . Only if the data can flow in correctly 、 Under the premise of outflow module , Other tests make sense .
The following factors should be considered to test whether the interface is correct :
1 Whether the number of input actual parameters is the same as that of formal parameters ;
2 Whether the input actual parameters match the properties of the formal parameters ;
3 Whether the dimensions of the input actual parameters and formal parameters are consistent ;
4 Whether the number of actual parameters given when calling other modules is the same as the number of formal parameters of the called module ;
5 Whether the attributes of the actual parameters given when calling other modules match the formal parameter attributes of the called module ;
6 Whether the dimension of the actual parameters given when calling other modules is consistent with the shape parameter dimension of the module being called ;
7 The number of arguments used to call a predefined function 、 Whether the attributes and order are correct ;
8 Whether there is a parameter reference independent of the current entry point ;
9 Whether the read-only parameter has been modified ;
10 Whether the definition of the whole process variable is consistent with each module ;
11 Whether to pass some constraints as parameters .
If external inputs and outputs are included in the module , The following factors should also be considered :
1 Whether the file properties are correct ;
2 OPEN/CLOSE Is the statement correct ;
3 Whether the format description matches the input / output statement ;
4 Whether the buffer size matches the record length ;
5 Whether the file has been opened before use ;
6 Whether the end of the file has been processed ;
7 Is the input processed / Output error ;
8 Whether there is any literal error in the output information ;
Checking the local data structure is to ensure that the data temporarily stored in the module is complete during the execution of the program 、 correct .
Local data structures are often the root cause of errors , Test cases should be carefully designed , Try to find the following types of errors :
1 Description of inappropriate or incompatible types ;
2 Variable has no initial value ;
3 Error in variable initialization or missing value ;
4 Incorrect variable name ( Misspelled or incorrectly truncated );
5 Overflow 、 Underflow and address exceptions .
In addition to local data structures , If possible , You should also check the global data during unit testing ( for example FORTRAN Public area of ) Impact on modules .
Each independent execution path shall be tested in the module , The basic task of unit testing is to ensure that each statement in the module is executed at least once . At this time, the test case is designed to find out that the calculation is wrong 、 Errors caused by incorrect comparison and improper control flow . At this time, basic path testing and loop testing are the most commonly used and effective testing techniques .
Common errors in calculations include :
1 Misunderstood or misused operator priority ;
2 Mixed type operation ;
3 The initial value of the variable is wrong ;
4 Insufficient precision ;
5 The expression symbol is wrong .
Comparative judgment and control flow are often closely related ,
The test case should also focus on finding the following errors :
1 Compare objects of different data types ;
2 Incorrect use of logical operators or priorities ;
3 Due to the limitations of computer representation , Two quantities that are theoretically equal but actually unequal are expected to be equal ;
4 Error in comparison operation or variable ;
5 Loop termination conditions may or may not occur ;
6 You cannot exit while iterating ;
7 Incorrectly modified the loop variable .
A good design should be able to foresee all kinds of error conditions , And preset various error handling channels , The error handling path also needs to be carefully tested ,
The test should focus on the following problems :
1 The output error message is difficult to understand ;
2 The recorded error does not match the error actually encountered ;
3 Before the error handling segment customized by the program runs , The system has intervened ;
4 Improper exception handling ;
5 The Error statement did not provide enough location error information .
Boundary condition test is the last of unit test , It is also the most important task . Everyone knows , Software often fails at the boundary , Using boundary value analysis technology , For boundary values and their left 、 Right design test case , It is possible to find new mistakes .
(3) Unit test process
It is generally believed that unit testing should be immediately after coding , When the source program is compiled and passes the review and compilation check , You can start unit testing . The design of test cases should be combined with the review , Select test data according to design information , It will increase the possibility of discovering the above errors . While determining test cases , The desired results should be given .
A driver module should be developed for the test module (driver) and ( or ) Several pile modules (stub), The following figure shows the general unit test environment . The drive modules are collectively referred to as... In most fields “ The main program ”, It receives test data and passes it to the module under test , After the tested module is called ,“ The main program ” Print “ Get into - sign out ” news .
The drive module and pile module are the software used in the test , Not part of a software product , But it requires a certain development cost . If the drive and pile modules are relatively simple , The actual cost is relatively low . However, only using simple driver module and pile module can not complete the test task of some modules , The unit test of these modules can only adopt the comprehensive test method discussed below .
Improving the cohesion of modules can simplify unit testing , If each module can only complete one , The number of test cases required will be significantly reduced , Errors in the module are easy to find .
边栏推荐
- [Galaxy Kirin V10] [server] NUMA Technology
- Dichotomy search (C language)
- [advantages and disadvantages of outsourcing software development in 2022]
- MFC document view framework (relationship between classes)
- Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
- Three schemes of ZK double machine room
- Using SA token to solve websocket handshake authentication
- Introduction to tree and binary tree
- JMeter Foundation
- The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
猜你喜欢
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
Number of relationship models
RHCE day 3
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
Latex learning insertion number - list of filled dots, bars, numbers
When I forget how to write SQL, I
[Galaxy Kirin V10] [server] failed to start the network
C language structure to realize simple address book
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
Four characteristics and isolation levels of database transactions
随机推荐
Es advanced series - 1 JVM memory allocation
Time complexity and space complexity
Virtual machine configuration network
/*Write a loop to output the elements of the list container in reverse order*/
DDL language of MySQL database: create, modify alter, delete drop of databases and tables
Basic function exercises
Talk about scalability
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
Communication layer of csframework
OSPF summary
Huge number multiplication (C language)
Get the data of the top 100 headlines today with Tianxing data
Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
Strings and characters
Introduction to extensible system architecture
leetcode842. Split the array into Fibonacci sequences
Fundamentals of database operation
VI text editor and user rights management, group management and time management
Snake (C language)