What Is Test Coverage?
Test coverage measures how much of your code is exercised by your tests, usually as a percentage of lines, branches, or functions.
Test coverage is a popular but easily misread metric. It tells you which parts of the code your tests touched while running, which is useful for spotting untested areas. What it cannot tell you is whether those tests actually assert the right things, so it is a starting point for judgment, not a substitute for it.
What coverage measures
A coverage tool instruments your code, runs the test suite, and records which statements, branches, or functions executed. The result is a percentage and a per-file breakdown showing exactly which lines were never reached by any test.
Why high coverage is not the goal
Code can be executed by a test without being meaningfully checked. A test that calls a function but asserts nothing still counts toward coverage. So 100 percent coverage with weak assertions can be worse than 70 percent with sharp ones. Coverage shows what is untested, not what is well-tested.
Using coverage well
- Treat low coverage as a flashlight pointing at gaps.
- Do not chase a vanity number with trivial tests.
- Watch the trend over time, not just the absolute figure.
- Focus coverage on risky, complex, or critical code.
A quick example
Tools like coverage.py or nyc print a report you can read at a glance, with the lines that no test reached called out per file.
Name Stmts Miss Cover
----------------------------------
cart.py 42 3 93%
checkout.py 58 12 79%Coverage gates in CI
Many pipelines fail a build if coverage drops below a threshold or regresses on changed lines. That keeps untested code from sneaking in. Generating and uploading the report adds a little time, which fast runners absorb so the gate does not slow the feedback loop.
Key takeaways
- Coverage measures which code your tests execute, as a percentage.
- High coverage does not guarantee good assertions or good tests.
- Use it to find gaps and watch trends, not as a vanity target.