Branch testing is a type of white-box testing that is used to test every possible branch in the control flow graph of a program. In branch testing, every branch in the code is executed at least once.
We need branch testing because:
Branch testing of a program is carried out by calculating a metric called branch coverage. Branch coverage is the ratio of the number of branches covered to the total number of branches. Branch coverage can be calculated as:
Consider the code snippet below, which is a simple program to find the maximum of two numbers:
int a = 2;int b = 5;if(a>b){cout<<"a is max";}else{cout<<"b is max";}
The control flow graph of the code above will be as follows:
The branches in the control flow graph above are A, B, C, D, E, F, and G.
For maximum branch coverage, we take the path:
Path1 = 1A-2B-3C-4D-5F-7
The branch coverage of path1 will be:
Branch coverage = (number of branches covered / total number of branches) x 100
Branch coverage = (5 / 7) x 100
Branch coverage = 71%
Branches E and G are not covered in path1. To cover branches E and G, we take the path:
Path2 = 1A-2B-3C-4E-6G-7
The branch coverage of path2 will be:
Branch coverage = (number of branches covered / total number of branches) x 100
Branch coverage = (5 / 7) x 100
Branch coverage = 71%
For 100% branch coverage, we combine both paths, path1 and path2. The final branch testing of the control flow graph above becomes: