How-To Guide

How to Measure Test Coverage Meaningfully

Use coverage as a signal, not a goal: track line and branch coverage, watch the delta, and judge the tests by what they assert.

On this page

  1. Introduction
  2. Measure line and branch coverage
  3. Watch the delta, not just the absolute
  4. Inspect the uncovered code, do not just count it
  5. Judge the tests by what they assert
  6. Test the coverage gate itself
  7. Common pitfalls
  8. Conclusion

Introduction

Test coverage measures which lines or branches ran during the tests. It is a signal about what was not run, not a measure of test quality: a line can run under a test that asserts nothing and be effectively untested, and a suite can have high line coverage while every error path is unexercised. Used well, coverage finds the code nobody tested; used as a target, it invites tests that run code without checking it.

This tutorial covers measuring line and branch coverage, watching the delta on a change, inspecting the uncovered code, and judging the tests by assertions. The examples use a coverage tool, and the reasoning applies to any coverage report.

Measure line and branch coverage

Line coverage reports which statements ran; branch coverage reports which paths of a conditional ran. Branch coverage is stricter and more useful, because a line can be covered while one branch of its condition never runs. An if with a covered then but an untested else has full line coverage and a half-covered branch, and the untested else is often the error or the boundary case.

Enable both and read the branch gaps. A branch gap is the interesting one: the code path that never ran is the code whose behavior is unverified. A line gap is a function or a block never reached, which may be dead code or an unexercised feature. Both are a to-do list, and branch coverage is the one that points at the risky paths.

# generate coverage with branch information
npx vitest run --coverage
# or: go test -coverprofile=cover.out ./... && go tool cover -func=cover.out
# branch % is the number to watch, not only lines

Watch the delta, not just the absolute

A high absolute coverage number can hide a decline: each change adds code, and if the new code is not covered, the percentage drops a little. A high starting point masks this until it falls below a threshold, which is too late to notice the trend. Watch the delta on a change: the new and modified lines should be covered, whatever the overall number.

A common gate is a coverage threshold on the changed lines, so a pull request that adds uncovered code fails even if the repository’s total is high. This keeps the coverage from eroding and puts the requirement where the new code is. The absolute number is a coarse health signal; the delta is what a change controls.

Inspect the uncovered code, do not just count it

The uncovered lines are the useful output, not the percentage. Read them: an uncovered line is often a catch block, an error branch, a boundary comparison, or a rare input, which is exactly a test that should exist. Open the coverage report and read the uncovered branches, because a number says how much is uncovered and the report says which code, and the which is actionable.

Prioritize the uncovered code by risk: an error path in a payment flow matters more than an untested logging line. Read the uncovered lines and decide for each whether it needs a test, is dead code to remove, or is genuinely untestable and should be covered by an explicit ignore with a reason. The review of the uncovered code is where coverage pays off.

# read the uncovered lines per file: branch gaps point at riskier paths
npx nyc report --reporter=text --reporter=html
# or open the html report and read the red branches, e.g. an untested catch

Judge the tests by what they assert

A covered line with no assertion is untested: the test ran the line but did not check the result, so a bug in that line would pass. Coverage counts the line as covered, which is why the percentage alone is misleading. When reviewing, check that each test asserts an outcome, and treat a test with no assertion as a defect.

Read the tests for the behavior they check, not the count of lines they touch. A test suite with fewer, focused tests that assert outcomes is worth more than one with many that call code and assert nothing. Coverage finds the code nobody ran; the review of what the tests assert is what makes the coverage meaningful.

Test the coverage gate itself

Test that the coverage gate fails on uncovered new code, by adding an uncovered branch and asserting CI fails, so the gate is actually enforced and not misconfigured. Test that a covered but unasserted test is not counted as meaningful, by mocking a return and asserting the test does not catch the change. These meta-tests keep the coverage practice honest.

# a test that asserts nothing still covers the line: coverage alone does not catch it
# review: replace the call with a wrong result and confirm the test would still pass
node -e '
// if a test asserts nothing, mutating the function does not fail the test
console.log("review the assertions, not the count");
'

Common pitfalls

Treating the coverage percentage as the goal, tracking only line coverage so branches are hidden, ignoring the delta so coverage erodes slowly, counting a covered but unasserted line as tested, and not reading the uncovered code so the error paths stay unexercised are the recurring mistakes. Line and branch coverage, a delta gate, reading the uncovered code, judging by assertions, and a meta-test of the gate address them.

Conclusion

Coverage is a signal about what was not run, not a measure of quality. Track line and branch coverage, watch the delta on a change, read the uncovered code to find the untested error and boundary paths, and judge the tests by what they assert. Self-check by adding an uncovered branch and asserting the gate fails, and by confirming a no-assertion test is treated as untested. Then coverage points at the code that needs a test instead of inviting tests that only run code.

Sources: Martin Fowler: Test coverage, Istanbul/NYC: Coverage, Go: Coverage profiling.

FAQ

Is high coverage the goal?
No. Coverage says which lines ran, not whether the tests assert the right thing. A line can run under a test with no assertion and be effectively untested. Use coverage to find uncovered code, and judge the tests by what they assert.
Which is more useful, line or branch coverage?
Branch coverage, because it flags an untested path such as the else of an if or the catch of a try, which line coverage hides. A line is covered if it runs once, even if a branch never takes the other path, and the untested branch is often the error case.

Related Examples