Sub topic 6
What coverage measures and what it does not
This chapter explains what code coverage numbers mean, so you can read them correctly before you generate reports and set thresholds in later chapters. It assumes you can run a test suite from the command line, read a basic GitHub Actions workflow, and use pytest.
What coverage is
Code coverage is a measurement of which parts of your source code ran while the tests ran. A coverage tool watches the program as the tests execute and records every line or branch that was reached. It then compares that record with the full list of lines or branches in your code.
Coverage tells you what the tests executed. It does not tell you whether the tests checked the results.
Line coverage and branch coverage
The two common metrics are line coverage and branch coverage.
- Line coverage is the share of executable lines that ran at least once.
- Branch coverage is the share of possible decision outcomes that ran. Each
ifhas two outcomes: the condition is true, or it is false. Loops andtryblocks have outcomes too.
Branch coverage is stricter. Here is a small function.
def apply_discount(price, is_member):
if is_member:
price = price * 0.9
return priceA single test with is_member=True runs every line, so line coverage is 100 percent. The case where is_member is false never ran, so branch coverage is lower. With coverage.py, you turn on branch measurement with the --cov-branch option of pytest-cov.
pytest --cov=src --cov-branch --cov-report=term-missingThe term-missing report prints the line numbers that were not run. With branch measurement on, it also shows partial branches.
What coverage does not tell you
A high number can still hide weak tests. Keep these limits in mind.
- A line can run without being checked. A test that calls a function and asserts nothing still counts as coverage.
- Coverage only sees code that is loaded. A file that no test imports may be missing from the report entirely, which makes the total look better than it is. Setting
--cov=srcmakes the tool measure the whole package. - Coverage does not measure inputs. One run through a line says nothing about edge cases such as empty lists, zero, or very large values.
- Coverage does not measure requirements. Code can be fully covered and still not do what users need.
The test below reaches 100 percent line coverage of apply_discount for the member case but proves nothing.
def test_discount_runs():
apply_discount(100, True)It never checks the result. A better test asserts the value.
def test_member_gets_ten_percent_off():
assert apply_discount(100, True) == 90Using the number well
Treat coverage as a way to find untested code, not as proof of quality. A low number is useful information. A high number is only weak evidence. When you review a report, look at the uncovered lines first, and ask whether those lines matter.
A threshold, covered later in this tutorial, is a guard against the number drifting down. It is not a goal to chase. Teams that push the percentage up with assertion-free tests get a better number and no better software.
Exercise
A function has one if statement with no else. One test makes the condition true. What are the line coverage and branch coverage likely to be, and why do they differ?
Show answer
Line coverage can be 100 percent, because every line ran, including the body of the if. Branch coverage is lower, because the case where the condition is false never ran. Branch coverage counts each outcome of the decision, not only each line.
Exercise
A test calls a function but contains no assert statements, and it passes. Does coverage count the function's lines as covered? Does that mean the function is tested?
Show answer
Yes, the lines count as covered, because they ran. No, the function is not meaningfully tested, because nothing checked its output. Coverage measures execution, not correctness.