Diagnostic Quiz
Problem
Branch Test Coverage
Suppose you have the following branch
if (Condition A) { // Decision Point 1
// Block 1
} else if (Condition B) { // Decision Point 2
// Block 2
} else {
// Block 3
}And you have two test cases which enter the else-if block and the else block, what will be your branch test coverage?
Ans: 75%. The formula to calculate the branch test coverage is as follows,
There are 4 distinct branches (outcomes) in this problem:
Branch 1:
Condition Ais True (Enters Block 1).Branch 2:
Condition Ais False (Proceeds to checkelse if).Branch 3:
Condition Bis True (Enters Block 2).Branch 4:
Condition Bis False (Enters Block 3/Else).
Now, let's map our specific test cases to these branches.
Test Case 1: Executes the
else ifblockFor the code to reach the
else ifblock,Condition Amust be False. (Covers Branch 2)Then,
Condition Bmust be True. (Covers Branch 3)
Test Case 2: Executes the
elseblockFor the code to reach the
else,Condition Amust be False. (Covers Branch 2 again)Then,
Condition Bmust be False. (Covers Branch 4)
So, the number of executed branches is 3, and as the total is 4, the branch test coverage will be 75%.
This question has appeared in CS2113 AY25/26 Sem 1 Final!
Last updated