0
0
PyTesttesting~5 mins

Branch coverage in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?<br>
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:<br>pytest --cov=your_module --cov-branch<br>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
Which pytest option enables branch coverage measurement?
A--test-branch
B--cov-branch
C--full-coverage
D--branch-test
If an if-statement has two branches, how many tests are needed for full branch coverage?
ATwo tests
BOne test
CThree tests
DNo tests
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
Which of these is NOT a decision point for branch coverage?
Avariable assignment
Bif statement
Cwhile loop condition
Dternary operator
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.