Statement Coverage Testing
Statement coverage is one of the widely used software testing. It comes under white box testing.
Statement coverage technique is used to design white box test cases. This technique involves execution of all statements of the source code at least once. It is used to calculate the total number of executed statements in the source code out of total statements present in the source code.
Statement coverage derives scenario of test cases under the white box testing process which is based upon the structure of the code.
In white box testing, concentration of the tester is on the working of internal source code and flow chart or flow graph of the code.
Generally, in the internal source code, there is a wide variety of elements like operators, methods, arrays, looping, control statements, exception handlers, etc. Based on the input given to the program, some code statements are executed and some may not be executed. The goal of statement coverage technique is to cover all the possible executing statements and path lines in the code.
Let’s understand the process of calculating statement coverage by an example:
Here, we are taking source code to create two different scenarios according to input values to check the percentage of statement coverage for each scenario.
Source Code Structure:
- Take input of two values like a=0 and b=1.
- Find the sum of these two values.
- If the sum is greater than 0, then print “This is the positive result.”
- If the sum is less than 0, then print “This is the negative result.”
So, this is the basic structure of the program, and that is the task it is going to do.
Now, let’s see the two different scenarios and calculation of the percentage of Statement Coverage for given source code.
Scenario 1:
If a = 5, b = 4
In scenario 1, we can see the value of sum will be 9 that is greater than 0 and as per the condition result will be “This is a positive result.” The statements highlighted in yellow color are executed statements of this scenario.
To calculate statement coverage of the first scenario, take the total number of statements that is 7 and the number of used statements that is 5.
Likewise, in scenario 2,
Scenario 2:
If A = -2, B = -7
In scenario 2, we can see the value of sum will be -9 that is less than 0 and as per the condition, result will be “This is a negative result.” The statements highlighted in yellow color are executed statements of this scenario.
To calculate statement coverage of the first scenario, take the total number of statements that is 7 and the number of used statements that is 6.
Total number of statements = 7
Number of executed statements = 6
But, we can see all the statements are covered in both scenario and we can consider that the overall statement coverage is 100%.
So, the statement coverage technique covers dead code, unused code, and branches.