当前位置:网站首页>How to write unit test cases
How to write unit test cases
2022-07-04 08:59:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
One 、 unit test The concept of
Generally speaking, a unit refers to a function that implements simple functions . Unit testing is to use only a specific set of inputs ( The test case ) Test whether the function functions normally , And returned the correct output .
Type of coverage tested
1. Statement override : Statement coverage is to design several test cases , Run the program under test , Make each executable statement execute at least once .
2. Determine coverage ( Also called branch coverage ): Design several test cases , Run the program under test , Make each true branch and false branch of judgment in the program execute at least once .
3. Conditional coverage : Design enough test cases , Run the program under test , Make each possible value of each condition of each judgment in the program execute at least once .
4. determine —— Conditional coverage : Design enough test cases , Run the program under test , Make each possible value of each condition of each judgment in the program execute at least once , And each possible judgment result is executed at least once .
5. Conditional combination test : Design enough test cases , Run the program under test , Make the combination of all conditional values of each judgment in the program execute at least once .
6. Path test : Design enough test cases , Run the program under test , To override all possible paths in the program .
The main design schemes of use cases are as follows : Conditions for testing , Basic path testing , Cycle test . Through the above method, the logic coverage of the test case to the program can be realized , And path coverage .
Two 、 Preparation before starting the test
At the beginning of the test , Let's make a statement first , No matter how many test cases you design , No matter how perfect your test plan is , Can't completely 100% All found BUG, What we need to do is use the least resources , Do the most tests and checks , Find a balance point to ensure the correctness of the program . Exhaustive testing is impossible . Therefore, for unit testing, I choose the basic path testing method commonly used now .
3、 ... and 、 Start testing
Basic path testing : The designed test cases should ensure that each basic independent path should be executed at least once .
Function description : When i_flag=0; return i_count+100
When i_flag=1; return i_count*10
Otherwise return to i_count*20
Input parameters :inti_count,
inti_flag
Output parameters :inti_return;
Code :
1intTest(inti_count,inti_flag) 2{ 3inti_temp=1; 4while(i_count>0) 5{ 6if(0==i_flag) 7{ 8i_temp=i_count+100; 9break; 10} 11else 12{ 13if(1==i_flag) 14{ 15i_temp=i_temp*10; 16} 17else 18{ 19i_temp=i_temp*20; 20} 21} 22i_count–; 23} 24returni_temp; 25} |
---|
1. Draw a program control flow chart
legend :
Case program flow chart :
The number in the circle represents the line number of the statement , Maybe someone asked why 4,6,13,8…… As nodes , The first 2 That's ok , The first 3 What behavior is not a node , Because the selection of nodes is regular . Let's look at the program ; The first 2 That's ok , The first 3 The rows are executed in order . Until the first 4 Then there is the circular operation . and 2,3 OK, no judgment , Select branch operation , So we put 2,3,4 Merge all into one node . Others are merged according to this rule , Then there is the flow chart above .
2. Computational cyclomatic complexity
With the diagram, we need to know how many test cases we have written , To meet the basic path test .
Here's a new concept —— Cyclomatic complexity
Cycle complexity is a software measure that provides quantitative test for program logic complexity . This metric is used to calculate the number of basic independent paths of the program . An upper bound on the number of tests to ensure that all statements are executed at least once .
Formula cycle complexity V(G)=E-N+2,E Is the number of edges in the flow diagram ,N Is the number of nodes in the flow diagram .
Formula cycle complexity V(G)=P+1,P It's a flow chart G Determines the number of nodes .
Generally speaking, circle responsibility is to judge whether the unit is complex , Is it a good test standard . Generally speaking, if the cyclomatic complexity is greater than 20 This means that the testability of this unit is not good , Too complicated ( Maybe some people think it doesn't matter , But if your company implements CMMI5 Words , There are rules for this ).
We can see from the picture ,
V(G)=10 side -8 node +2=4
V(G)=3 Two decision nodes +1=4
The circle complex graph in the above figure is 4. What does this result mean to us ? It means that we only need... At most 4 One test case can achieve basic path coverage .
3. Export program basic path .
Now we know, at least write 4 Test cases , But how to design this 4 Test cases ?
Export program basic path , Design test examples according to the basic path of the program .
Program basic path : The basic independent path is that you can choose any path to traverse from the beginning node to the end of the program , But each path should contain at least one edge that the defined path does not use .( It seems hard to understand , Let's look at an example ).
Let's look at the flow chart above : From the node 4 To 24 There are several paths ?
1B(4,24)
2C,E,J(4,6,8,24)
3C,D,F,H,A,B(4,6,13,15,22,4,24)
4C,D,G,I,A,B(4,6,13,19,22,4,24)
anything else ??
5C,D,C,I,A,C,E,J(4,6,13,19,22,4,6,8,24) Is that right ?
not , Why? ? Because of the above 4 This path already includes all the edges . The first 5 This path no longer contains unused edges . All paths have been traversed .
Okay , Now we have 4 Basic independent path according to which we can design test cases .
1B(4,24)
input data :i_count=0, Or is it i_count<0 A certain value of .
Expected results :i_temp=0.
2C,E,J(4,6,8,24)
input data :i_count=1;i_flag=0
Expected results :i_temp=101.
3C,D,F,H,A,B(4,6,13,15,22,4,24)
input data :i_count=1;i_flag=1
Expected results :i_temp=10.
4C,D,G,I,A,B(4,6,13,19,22,4,24)
input data :i_count=1;i_flag=2
Expected results :i_temp=20.
The input data here is inferred from the path and program . Note that the expected result is derived from the function description , Cannot export... From program structure .
Why do you say that? ?
Let's look at the second part of the program 3 That's ok .
inti_temp=1; If a developer accidentally makes a mistake , Turned into inti_temp=0; The expected result derived from the program will be an incorrect value , But unit testing doesn't work .
Then the unit test is meaningless .
Someone might ask that such a simple function has 4 Test cases , What if it's more complicated ? Can the above test cases be simplified ? The answer is yes .
Let's look at the path 1B(4,24) and 4C,D,G,I,A,B(4,6,13,19,22,4,24), route 1 It's the path 4 The proper subset of , therefore 1 It can be unnecessary . The cycle complexity of the above figure is 4. What does this result mean to us ? It means that we only need... At most 4 One test case can achieve basic path coverage . So the cyclomatic complexity indicator is the largest number of test cases , Not necessarily 4 Only one test case can . However, one thing to be clear is that the simpler the test cases, the less your tests , The less secure the program is .
Four 、 To complete the test
Next, use the tools to test according to the test cases NUNIT,VS2005 Fine .
Next, write a test report according to the test results , Tested by , Time , result , Use cases , Whether to pass , Format a lot of online , The format of each company is different, so don't say .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/148660.html Link to the original text :https://javaforall.cn
边栏推荐
- Codeforces Global Round 21(A-E)
- China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
- C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- Sword finger offer 30 contains the stack of Min function
- C语言-入门-基础-语法-数据类型(四)
- Awk from getting started to digging in (4) user defined variables
- awk从入门到入土(15)awk执行外部命令
- Cancel ctrl+alt+delete when starting up
- awk从入土到入门(10)awk内置函数
猜你喜欢
High order phase difference such as smear caused by myopic surgery
How to ensure the uniqueness of ID in distributed environment
System disk expansion in virtual machine
Ehrlich sieve + Euler sieve + interval sieve
【无标题】转发最小二乘法
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Dede plug-in (multi-function integration)
How to choose solid state hard disk and mechanical hard disk in computer
C language - Introduction - Foundation - syntax - data type (4)
How to play dapr without kubernetes?
随机推荐
Flutter tips: various fancy nesting of listview and pageview
Mac platform forgets the root password of MySQL
微服务入门:Gateway网关
ctfshow web255 web 256 web257
Implementation principle of redis string and sorted set
How college students choose suitable computers
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
FOC control
A subclass must use the super keyword to call the methods of its parent class
Educational Codeforces Round 115 (Rated for Div. 2)
Codeforces Round #793 (Div. 2)(A-D)
【无标题】转发最小二乘法
awk从入门到入土(14)awk输出重定向
Clion console output Chinese garbled code
09 softmax regression + loss function
《网络是怎么样连接的》读书笔记 - FTTH
Educational Codeforces Round 119 (Rated for Div. 2)
How to play dapr without kubernetes?
GoLand environment variable configuration
Awk from getting started to digging in (4) user defined variables