Branch Coverage measures the number of branches taken in proportion to the number of conditional branches present. The branches are counted as the total number of originating edges from decision nodes in the program. A branch is identified as a transition between a pair of statements (a source and a destination).
Reachable branches
Code coverage is computed on Reachable Branches only. That is, if you have 10 branches, but only 8 are reachable, and you have covered 6 of them, your branch coverage is 6/8. Not 6/10.
Structurally partial branch
Example 1 - Ifs
1. def f_if(a):
2. if a:
3. print("a")
4. else:
5. print("b")
Q: How many branches are there in the f_if
?
A: 2
- line 2 →
print('a')
line 3 - line 2 →
else
line 5
Example 2 - Elifs
1. def f_elif(a, b):
2. if a:
3. print('a')
4. elif b:
5. print('b')
6. else:
7. print('c')
Q: how many branches are there in f_elif? A: 4
- line 2 →
print('a')
line 3 - line 2 →
elif b
line 4 - line 4 →
print('b')
line 5 - line 4 →
print('c')
line 7
Example 3 - Loops
1. def f_for(xs):
2. for x in xs:
3. print(x)
4. print('Done')
Q: How many branches are there in f_for? A: 2
- line 2 →
print(x)
line 3 - line 2 →
print('Done')
line 4
Q: would a single test casef_for([1,2,3])
cover both branches?
A: Yes, the non-empty list would cause the “true” branch to be taken. On exiting the loop, the “false” branch is taken.
Back to parent page: Software Testing
Web_and_App_Development Software_Testing Whitebox_Testing SOFT3202 Branch_Coverage