0
0
PyTesttesting~20 mins

Branch coverage in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Branch Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"
ATest passes but branch coverage is 50%
BTest passes and branch coverage is 100%
CTest fails due to assertion error
DTest passes but branch coverage is 0%
Attempts:
2 left
💡 Hint
Branch coverage means each branch of if-else is executed at least once.
assertion
intermediate
2: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
Aassert is_even(2) and not is_even(3)
Bassert is_even(2) == True
Cassert is_even(3) == False
Dassert is_even(0) == True
Attempts:
2 left
💡 Hint
To cover both branches, test both even and odd numbers.
🔧 Debug
advanced
2: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"
AThe 'Minor' branch is not covered
BThe 'Adult' branch is not covered
CThe 'Senior' branch is not covered
DAll branches are covered
Attempts:
2 left
💡 Hint
Check which return statements are executed by the test inputs.
framework
advanced
2:00remaining
Using pytest-cov for branch coverage
Which pytest command correctly runs tests with branch coverage reporting?
Apytest --cov=my_module --cov-branch
Bpytest --cov=my_module --branch
Cpytest --coverage=my_module --branch
Dpytest --cov-branch my_module
Attempts:
2 left
💡 Hint
The option for branch coverage is '--cov-branch' with pytest-cov.
🧠 Conceptual
expert
2:00remaining
Understanding branch coverage impact
Why is branch coverage considered a stronger metric than statement coverage in testing?
ABecause branch coverage only counts executed lines, ignoring conditions
BBecause branch coverage measures performance of the code during tests
CBecause statement coverage requires testing all input values, branch coverage does not
DBecause branch coverage ensures every possible path decision is tested, not just each line
Attempts:
2 left
💡 Hint
Think about what branch coverage tracks compared to statement coverage.