The statement coverage technique is used to design test cases for white box testing which involves the execution of all the statements of the source code at least once. This article focuses on discussing statement coverage in detail.
What is Statement Coverage?
It is one type of white box testing technique that ensures that all the statements of the source code are executed at least once. It covers all the paths, lines, and statements of a source code. It is used to design test box cases where it will find out the total number of executed statements out of the total statements present in the code.
Formula:
Statement coverage = (Number of executed statements / Total number of statements in source code) * 100
Example 1:
Read A
Read B
if A > B
Print “A is greater than B”
else
Print “B is greater than A”
endif
Case 1:
If A = 7, B= 3
No of statements Executed= 5
Total statements= 7
Statement coverage= 5 / 7 * 100
= 71.00 %
Case 2:
If A = 4, B= 8
No of statements Executed= 6
Total statements= 7
Statement coverage= 6 / 7 * 100
= 85.20 %
Example 2:
print (int a, int b)
{
int sum = a + b;
if (sum > 0)
print (“Result is positive”)
else
print (“Result is negative”)
}
Case 1:
If A = 4, B= 8
No of statements Executed= 6
Total statements= 8
Statement coverage= 6 / 8 * 100
= 75.00 %
Case 2:
If A = 4, B= -8
No of statements Executed= 7
Total statements= 8
Statement coverage= 7 / 8 * 100
= 87.50 %
In the internal code structure, there are loops, arrays, methods, exceptions, and control statements. Some code would be executed based on input while some may not. Statement coverage will execute all possible paths and statements of the code
Statement coverage covers:
- Dead code.
- Unused statements.
- Unused branches.
- Missing statements.
Why is statement coverage used?
- To check the quality of the code.
- To determine the flow of different paths of the program.
- Check whether the source code expected to perform is valid or not.
- tests the software’s internal coding and infrastructure.
Drawback of Statement Coverage:
- Cannot check the false condition.
- Different input values to check all the conditions.
- More than one test case may be required to cover all the paths with a coverage of 100%.