Branch Coverage Testing
Branch coverage technique is used to cover all branches of the control flow graph. It covers all the possible outcomes (true and false) of each condition of decision point at least once. Branch coverage technique is a whitebox testing technique that ensures that every branch of each decision point must be executed.
However, branch coverage technique and decision coverage technique are very similar, but there is a key difference between the two. Decision coverage technique covers all branches of each decision point whereas branch testing covers all branches of every decision point of the code.
In other words, branch coverage follows decision point and branch coverage edges. Many different metrics can be used to find branch coverage and decision coverage, but some of the most basic metrics are: finding the percentage of program and paths of execution during the execution of the program.
Like decision coverage, it also uses a control flow graph to calculate the number of branches.
How to calculate Branch coverage?
There are several methods to calculate Branch coverage, but pathfinding is the most common method.
In this method, the number of paths of executed branches is used to calculate Branch coverage. Branch coverage technique can be used as the alternative of decision coverage. Somewhere, it is not defined as an individual technique, but it is distinct from decision coverage and essential to test all branches of the control flow graph.
Let’s understand it with an example:
This is the basic code structure where we took two variables X and Y and two conditions. If the first condition is true, then print “Large” and if it is false, then go to the next condition. If the second condition is true, then print “Small.”
Control flow graph of code structure
In the above diagram, control flow graph of code is depicted. In the first case traversing through “Yes “decision, the path is A1-B2-C4-D6-E8, and the number of covered edges is 1, 2, 4, 5, 6 and 8 but edges 3 and 7 are not covered in this path. To cover these edges, we have to traverse through “No” decision. In the case of “No” decision the path is A1-B3-5-D7, and the number of covered edges is 3 and 7. So by traveling through these two paths, all branches have covered.
Path 1 - A1-B2-C4-D6-E8 Path 2 - A1-B3-5-D7 Branch Coverage (BC) = Number of paths =2
|1, 2, 4, 5, 6, 8