当前位置:网站首页>Test basis: unit test

Test basis: unit test

2022-06-12 21:15:00 InfoQ

One 、 Definition

unit testing (Unit Testing), It refers to the inspection and verification of the smallest testable unit in the software . The test content includes module interface test 、 Local data testing 、 Boundary condition test 、 Error handling test, independent path test, etc .

In particular :
1.  All the code , General industry is to classify and process data .
2.  Not all code should be unit tested , Unit testing is usually used only in the testing of underlying modules or core modules .3.  The module interface test mainly includes whether the formal parameters that only do input are modified 、 Whether a variable that has not been initialized is used 、 Whether the number of parameters output to the standard function is correct .

Two 、 Common methods

The use case of unit testing is a “ input data ” and “ Output data ” Set .
input data , It can be the input parameter of the function under test 、 Global static variables that need to be read internally 、 Internal member variables to be read 、 The data obtained by calling the sub function inside the function 、 Data rewritten by calling sub functions inside the function, etc . The expected output is definitely not as simple as the return value of the function , It also includes all data rewritten after the function is executed . In unit test , Driver code -Driver, The pile of code -Stub  and  Mock  Code is the three most common nouns , Let's take a brief look at .

1、 Driver code -Driver

It refers to the code calling the function under test .

2、 The pile of code -Stub

Temporary code used to replace real code . The application of pile code first plays the role of isolation and supplement , Enable the tested code to compile independently 、 link , And run independently . meanwhile , Pile code also has the function of controlling the execution path of the function under test . Don't pay attention to  Stub  Whether and how to be called . Writing pile code usually follows the following principles :
1) The pile function should have the same prototype as the original function , Only the internal implementation is different , In this way, the test code can be correctly linked to the pile function ;
2) The installation function used to implement isolation and replenishment is relatively simple , Just keep the declaration of the original function , Add an empty implementation , Link by compiling ;
3) The pile function to realize the control function is the most widely used , According to the needs of test cases , Output appropriate data as the internal input of the measured function .

3、Mock Code

Focus on  Mock  Method has not been called , What parameters are called , Number of calls , And many  Mock  The sequence of sequential calls of functions .
null
Selection of test frame , Directly related to the development language .
Java  The most commonly used unit testing framework is  Junit  and  TestNG;
C/C++  The most commonly used unit testing framework is  CppTest  and  Parasoft C/C++Test;
After the frame selection is completed , You also need to work on the pile code framework and  Mock  Code framework selection , The main basis of type selection is the specific technology used in development . Usually , Unit test framework 、 The pile of code /Mock  The selection of code is decided by the development architect and the test architect .

If you have any questions in the reading process , Welcome to leave a message in the comments area to participate in the discussion !
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122105591440.html