I have purposefully skipped talking much about achieving 100% line coverage or branch coverage or other coverage. I do not believe that achieving a number should be the goal. Nevertheless, given how prevalent those numbers are in practice, it is important to understand them. First, let’s talk about the metrics themselves.
NOTE Formulas vary among the tools on the market. Check your tool’s manual to better understand the precise numbers you get.
If the entire test suite covers all the lines in the program (or in the class or method under test), that suite achieves 100% line coverage. A simple formula to calculate the line coverage of a given program or method is to divide the number of lines covered by the total number of lines:
You can calculate this number at the method level, class level, package level, system level, or whatever level you are interested in.
Similar to line coverage, a formula to calculate the achieved branch coverage of a program or method is the number of branches covered divided by the total number of branches:
In a simple program such as if(x)
{
do
A
}
else
{
do
B
}
, the total number of branches is two (the single if
statement branches the program in two ways). Therefore, if one test in your test suite covers, say x
=
true
, your test suite achieves 1/2 × 100% = 50% branch coverage. Note that due to criteria subsumption, which we discussed earlier, if you cover all the branches of the program, you also cover all the lines.
Finally, a formula to calculate the condition + branch coverage of a given program or method is the sum of all branches and conditions covered, divided by the total number of branches and conditions:
In a simple program such as if(x
||
y)
{
do
A
}
else
{
do
B
}
, the total number of branches is two (the single if
statement branches the program in two ways) and the total number of conditions is four (two conditions for x
and two conditions for y
). Therefore, if you have two tests in your test suite—T1: (true,
true)
and T2: (false,
true)
—the test suite achieves (1 + 3)/(2 + 4) × 100% = 66.6% condition + branch coverage. The test suite covers only one branch of the program (the true
branch, as both T1 and T2 make the if
expression evaluate to true
), and three of the four conditions (x
is exercised as true
and false
, but y
is only exercised as true
).
Figure 3.10 shows a simple illustration of line coverage, branch coverage, and condition + branch coverage. When someone says, “My test suite achieves 80% condition + branch coverage,” you now understand that 80% of the branches and conditions are covered by at least one test case. And when someone says, “My test suite achieves 100% line coverage,” you know that 100% of the lines are covered by at least one test case.
Figure 3.10 Two control-flow graphs of simple programs and how the different coverage criteria are calculated
Leave a Reply