if-else
) and unconditional paths (e.g., sequential execution).if
, switch
, loops) evaluates to all possible outcomes at least once.3. 100% Branch Coverage:
Consider the following code:
if (x > 0):
print("Positive")
else:
print("Non-positive")
print("Done")
Branches:
if (x > 0)
→ Trueif (x > 0)
→ FalseTest Cases for 100% Branch Coverage:
x = 5
→ True branch executed.x = -3
→ False branch executed.Result: Both branches are tested, achieving 100% branch coverage.
Branch testing is essential for ensuring code correctness, especially in decision-heavy logic. By subsuming statement coverage, it offers more robust test coverage.
Here are ISTQB-style multiple-choice questions (MCQs) on Branch Testing and Branch Coverage:
a) To execute all statements in the code
b) To execute all decision outcomes in the code
c) To execute all paths in the code
d) To execute all combinations of conditions
Answer: b) To execute all decision outcomes in the code
a) (Number of executed branches / Total branches) × 100
b) (Number of executed statements / Total statements) × 100
c) (Number of executed paths / Total paths) × 100
d) (Number of executed conditions / Total conditions) × 100
Answer: a) (Number of executed branches / Total branches) × 100
a) It guarantees all paths in the code are tested.
b) It ensures 100% statement coverage.
c) It detects all possible defects in the code.
d) It guarantees all combinations of inputs are tested.
Answer: b) It ensures 100% statement coverage.
if (x > 0):
print("Positive")
else:
print("Non-positive")
print("Done")
a) 1
b) 2
c) 3
d) 4
Answer: b) 2
a) All decision outcomes
b) All branches in the code
c) All sequential statements
d) All possible paths through the code
Answer: d) All possible paths through the code
a) Conditional branches only
b) Unconditional branches only
c) Both conditional and unconditional branches
d) Only branches with loops
Answer: c) Both conditional and unconditional branches
a) Branch coverage subsumes statement coverage.
b) Statement coverage subsumes branch coverage.
c) They are equivalent.
d) They measure the same aspect of testing.
Answer: a) Branch coverage subsumes statement coverage.
a) Missing statements
b) Dead code
c) Defects in a specific path
d) Incorrect decision logic
Answer: c) Defects in a specific path
a) Statement coverage will always be less than branch coverage.
b) Statement coverage will always be equal to branch coverage.
c) 100% statement coverage is guaranteed.
d) Statement coverage is irrelevant.
Answer: c) 100% statement coverage is guaranteed.
a) Testing the loop condition for true only
b) Testing the loop condition for false only
c) Testing the loop condition for both true and false outcomes
d) Executing the loop multiple times
Answer: c) Testing the loop condition for both true and false outcomes