Bird
Raised Fist0
PyTesttesting~20 mins

Branch coverage in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does branch coverage measure in pytest testing?

easy
A. It checks if all decision paths in the code are tested.
B. It measures how many lines of code are executed.
C. It counts the number of test cases written.
D. It verifies the syntax correctness of the test code.

Solution

  1. Step 1: Understand branch coverage concept

    Branch coverage ensures every possible path in decision points (like if-else) is tested.
  2. Step 2: Compare with other coverage types

    Line coverage counts executed lines, but branch coverage focuses on decision paths.
  3. Final Answer:

    It checks if all decision paths in the code are tested. -> Option A
  4. Quick Check:

    Branch coverage = all decision paths tested [OK]
Hint: Branch coverage tests all if-else paths, not just lines [OK]
Common Mistakes:
  • Confusing branch coverage with line coverage
  • Thinking it counts test cases
  • Assuming it checks syntax errors
2.

Which pytest command option enables branch coverage measurement?

easy
A. pytest --cov --cov-branch
B. pytest --branch-coverage
C. pytest --cov-branch
D. pytest --coverage-branch

Solution

  1. Step 1: Recall pytest coverage options

    The option --cov enables coverage measurement, and --cov-branch adds branch coverage.
  2. Step 2: Identify correct combined command

    Both options must be used together as pytest --cov --cov-branch to measure branch coverage.
  3. Final Answer:

    pytest --cov --cov-branch -> Option A
  4. Quick Check:

    Use --cov with --cov-branch for branch coverage [OK]
Hint: Use both --cov and --cov-branch together [OK]
Common Mistakes:
  • Using only --cov-branch without --cov
  • Typing incorrect option names
  • Assuming branch coverage is default
3.

Given this code snippet tested with pytest and branch coverage enabled, what is the branch coverage result?

def check_num(x):
    if x > 0:
        return "Positive"
    else:
        return "Non-positive"

# Test cases:
assert check_num(5) == "Positive"
medium
A. 100% branch coverage
B. 75% branch coverage
C. 0% branch coverage
D. 50% branch coverage

Solution

  1. Step 1: Identify branches in the code

    The function has two branches: one for x > 0 and one for else (x ≤ 0).
  2. Step 2: Check which branches are tested

    The test calls check_num(5), which covers only the x > 0 branch, missing the else branch.
  3. Final Answer:

    50% branch coverage -> Option D
  4. Quick Check:

    One branch tested out of two = 50% [OK]
Hint: Test all if-else paths to get 100% branch coverage [OK]
Common Mistakes:
  • Assuming one test covers all branches
  • Confusing line coverage with branch coverage
  • Ignoring else branch testing
4.

Identify the error in this pytest test that aims for full branch coverage:

def classify(n):
    if n > 10:
        return "High"
    elif n > 5:
        return "Medium"
    else:
        return "Low"

def test_classify():
    assert classify(12) == "High"
    assert classify(7) == "Medium"
medium
A. Syntax error in the test function.
B. Incorrect expected values in assertions.
C. Missing test for the else branch (n ≤ 5).
D. The function classify has no return statement.

Solution

  1. Step 1: Analyze branches in classify()

    There are three branches: n > 10, n > 5, and else (n ≤ 5).
  2. Step 2: Check test coverage of branches

    Tests cover n=12 (High) and n=7 (Medium), but no test covers n ≤ 5 (Low) branch.
  3. Final Answer:

    Missing test for the else branch (n ≤ 5). -> Option C
  4. Quick Check:

    All branches need tests for full coverage [OK]
Hint: Test all if, elif, else branches to avoid missing coverage [OK]
Common Mistakes:
  • Assuming two tests cover all branches
  • Looking for syntax errors where none exist
  • Ignoring else branch testing
5.

You have this function. Which set of test inputs achieves 100% branch coverage?

def analyze_score(score):
    if score >= 90:
        return "A"
    elif score >= 75:
        return "B"
    else:
        return "C"
hard
A. [90, 85, 76]
B. [95, 80, 70]
C. [91, 74, 60]
D. [89, 84, 73]

Solution

  1. Step 1: Identify all branches

    Branches: score ≥ 90 ("A"), score ≥ 75 ("B"), else "C".
  2. Step 2: Check which inputs cover all branches

    Input 95 covers "A" branch; 80 covers score ≥ 75 ("B"); 70 covers else "C" branch. This covers all branches.
  3. Final Answer:

    [95, 80, 70] -> Option B
  4. Quick Check:

    Test inputs cover all if-elif-else paths [OK]
Hint: Pick inputs hitting each if, elif, else branch [OK]
Common Mistakes:
  • Missing coverage for one of the branches
  • Choosing inputs that skip else branch
  • Assuming close values cover all branches