Which statement best describes branch coverage in software testing?
Think about decisions in code like if statements and their possible paths.
Branch coverage ensures that each decision's possible outcomes (branches) are tested, not just the lines of code.
Consider this Python function:
def check_number(x):
if x > 0:
return "Positive"
else:
return "Non-positive"If a test suite calls check_number(5) only, what is the branch coverage?
Think about which parts of the if statement run when x=5.
Only the true branch of the if was executed. The false branch was never tested.
Given this test code snippet for a function is_even(n) that returns True if n is even, which assertion best checks that both branches of the function are tested?
def test_is_even(): # Which assertions ensure full branch coverage? pass
To cover branches, test both even and odd inputs.
Testing only one input covers one branch. Testing both even and odd covers both branches.
Here is a function and its test cases:
def classify_age(age):
if age < 13:
return "Child"
elif age < 20:
return "Teen"
else:
return "Adult"
# Test cases
assert classify_age(10) == "Child"
assert classify_age(25) == "Adult"What is the branch coverage status of these tests?
Check if all if and elif branches have been tested.
The 'Teen' branch (age between 13 and 19) is never tested, so branch coverage is not 100%.
Using Python's unittest and coverage tools, which command correctly measures branch coverage for test_module.py?
Check the coverage tool's option for branch coverage.
The --branch flag enables branch coverage measurement during test execution.