In software testing, code coverage is a measure that determines how much code in a program is successfully tested. It provides various methods such as branch coverage, statement coverage,decision coverage, FSM coverage and more. In this article, we’ll be discussing branch coverage and statement coverage.
Branch coverage and Statement Coverage are form of white box testing techniques.
The main difference between them is, the aim of statement coverage is to traverse all statements at least once, whereas the goal of branch coverage it to traverse all the branches at least once.
Difference between Statement Coverage and Branch Coverage in Tabular form
Statement Coverage | Branch Coverage |
---|---|
Statement coverage is a technique which aims to cover all the statements at least once by executing the program | The main goal of branch coverage is to cover branches at least once (true and false) |
It focuses on covering all statements or lines of a program | It focuses on covering branches, both conditional and unconditional |
It is weaker criteria than branch coverage as test cases that cover all statements may not cover all the branches | It is stronger criteria than branch coverage that cover all branches would also cover all the statements too |
Coverage Measurement, Statement coverage = (Total Statements covered/Total Statements )* 100 | Coverage Measurement, Branch coverage = (Total branch covered/Total Branches )* 100 |
Statement Coverage
It is also termed as Line coverage.
The goal of this technique is to cover all the statements at least once by executing the program.
It requires test cases that make possible to run all the statement consisting of the program in order to achieve 100% coverage. It only assures that all the statements have executed but not assures whether all the paths have covered or not.
For example:
READ A IF A == 10 THEN PRINT I am True ElSE PRINT I am False ENDIF
Test case #1 ( A = 5 )
Statement coverage = (Total Statements covered/Total Statements )* 100
=(5/7)*100
In the above code, 71.5% statement coverage is achieved by test case #1.
Test case #2 ( A = 10 )
Statement coverage = (Total Statements covered/Total Statements )* 100
=(5/7)*100
Again, 71.5% statement coverage is covered, and altogether these two test cases executed all the possible paths with 71.5% statement coverage each.
Executing every statement of a program helps in finding faults, unreachable or useless statements.
Branch Coverage
In a flow graph, an arrow represents edges and counting a statement as a node, where two nodes are connected with an edge.
A diamond symbol represents the decision node where more than one edges are present in an outgoing manner which is termed as Branches.
The main aim of branch coverage is to cover all the branches ( two separate paths) at least once (true and false).
Branch Coverage is a structurally based technique that checks both conditional and unconditional branches.
Branch coverage = (number of executed branches/ total number of branches) * 100
For example:
READ A IF A == 10 THEN PRINT I am True ElSE PRINT I am False ENDIF
Branches:
- 2,5,6,7
- 2,3,4,7
To cover all the branches we would require 2 test cases:
Test case #1 ( A = 12 )
Branch coverage = (Total branch covered/Total Branches )* 100
=(1/2)*100
In the above code, 50% branch coverage is achieved by test case #1.
Test case #2 ( A = 10 )
Branch coverage = (Total branch covered/Total Branches )* 100
=(1/2)*100
Again, 50% branch coverage is achieved by test case #2.
Altogether these two test cases executed all the possible branches with 50% branch coverage each.