Challenge - 5 Problems
Branch Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Branch coverage with simple if-else
What is the output of the following pytest test when run with branch coverage enabled?
PyTest
def check_number(x): if x > 0: return "Positive" else: return "Non-positive" def test_check_number(): assert check_number(5) == "Positive" assert check_number(-1) == "Non-positive"
Attempts:
2 left
💡 Hint
Branch coverage means each branch of if-else is executed at least once.
✗ Incorrect
Both branches of the if-else are tested: x=5 covers the 'if' branch, x=-1 covers the 'else' branch. So branch coverage is 100%.
❓ assertion
intermediate2:00remaining
Assertion to ensure full branch coverage
Which assertion in pytest ensures that both branches of this function are tested for full branch coverage?
PyTest
def is_even(n): if n % 2 == 0: return True else: return False
Attempts:
2 left
💡 Hint
To cover both branches, test both even and odd numbers.
✗ Incorrect
Option A tests is_even with 2 (even) and 3 (odd), covering both branches. Other options test only one branch.
🔧 Debug
advanced2:00remaining
Identify missing branch coverage in test
Given this function and test, which branch is NOT covered?
PyTest
def classify_age(age): if age < 18: return "Minor" elif age < 65: return "Adult" else: return "Senior" def test_classify_age(): assert classify_age(10) == "Minor" assert classify_age(30) == "Adult"
Attempts:
2 left
💡 Hint
Check which return statements are executed by the test inputs.
✗ Incorrect
Test inputs cover 'Minor' and 'Adult' branches but never test age >= 65, so 'Senior' branch is not covered.
❓ framework
advanced2:00remaining
Using pytest-cov for branch coverage
Which pytest command correctly runs tests with branch coverage reporting?
Attempts:
2 left
💡 Hint
The option for branch coverage is '--cov-branch' with pytest-cov.
✗ Incorrect
Option A uses correct syntax: '--cov' to specify module and '--cov-branch' to enable branch coverage.
🧠 Conceptual
expert2:00remaining
Understanding branch coverage impact
Why is branch coverage considered a stronger metric than statement coverage in testing?
Attempts:
2 left
💡 Hint
Think about what branch coverage tracks compared to statement coverage.
✗ Incorrect
Branch coverage requires testing both true and false outcomes of each decision, ensuring more thorough testing than statement coverage which only checks if lines run.