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
Recall & Review
beginner
What is branch coverage in software testing?
Branch coverage measures whether each possible branch (true/false) in decision points like if-else statements has been executed at least once during testing.
Click to reveal answer
beginner
How does branch coverage differ from statement coverage?
Statement coverage checks if each line of code runs, but branch coverage ensures every decision path (true and false) is tested, catching more logic errors.
Click to reveal answer
intermediate
Why is branch coverage important in pytest tests?
Branch coverage helps find untested decision paths in your code, making tests more thorough and reducing bugs from missed conditions.
Click to reveal answer
beginner
Example: What branches exist in this code snippet?
if x > 0:
return 'positive'
else:
return 'non-positive'
There are two branches: one where x > 0 is true, and one where x > 0 is false (else). Both must be tested for full branch coverage.
Click to reveal answer
intermediate
How can you measure branch coverage in pytest?
Use pytest with the coverage plugin and run: pytest --cov=your_module --cov-branch This reports branch coverage along with line coverage.
Click to reveal answer
What does branch coverage ensure in testing?
AOnly the main function is tested
BEvery line of code is executed once
CEvery possible path in decision points is tested
DTests run faster
✗ Incorrect
Branch coverage ensures that all decision paths (true and false branches) are tested.
Which pytest option enables branch coverage measurement?
A--test-branch
B--cov-branch
C--full-coverage
D--branch-test
✗ Incorrect
The option --cov-branch tells pytest coverage to measure branch coverage.
If an if-statement has two branches, how many tests are needed for full branch coverage?
ATwo tests
BOne test
CThree tests
DNo tests
✗ Incorrect
Two tests are needed: one where the condition is true, one where it is false.
Branch coverage helps find bugs that statement coverage might miss because:
AIt ignores conditions
BIt runs tests faster
CIt only tests loops
DIt tests all decision outcomes
✗ Incorrect
Branch coverage tests all outcomes of decisions, catching logic errors missed by statement coverage.
Which of these is NOT a decision point for branch coverage?
Avariable assignment
Bif statement
Cwhile loop condition
Dternary operator
✗ Incorrect
Variable assignment is not a decision point; branch coverage focuses on conditions that create branches.
Explain branch coverage and why it is important in testing with pytest.
Think about how if-else decisions create different paths in code.
You got /3 concepts.
Describe how you would write tests to achieve full branch coverage for a function with multiple if-else statements.
Consider each if-else as a fork in the road that needs exploring.
You got /3 concepts.
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
Step 1: Understand branch coverage concept
Branch coverage ensures every possible path in decision points (like if-else) is tested.
Step 2: Compare with other coverage types
Line coverage counts executed lines, but branch coverage focuses on decision paths.
Final Answer:
It checks if all decision paths in the code are tested. -> Option A
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
Step 1: Recall pytest coverage options
The option --cov enables coverage measurement, and --cov-branch adds branch coverage.
Step 2: Identify correct combined command
Both options must be used together as pytest --cov --cov-branch to measure branch coverage.
Final Answer:
pytest --cov --cov-branch -> Option A
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
Step 1: Identify branches in the code
The function has two branches: one for x > 0 and one for else (x ≤ 0).
Step 2: Check which branches are tested
The test calls check_num(5), which covers only the x > 0 branch, missing the else branch.
Final Answer:
50% branch coverage -> Option D
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
Step 1: Analyze branches in classify()
There are three branches: n > 10, n > 5, and else (n ≤ 5).
Step 2: Check test coverage of branches
Tests cover n=12 (High) and n=7 (Medium), but no test covers n ≤ 5 (Low) branch.
Final Answer:
Missing test for the else branch (n ≤ 5). -> Option C
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"