Code Coverage
What is Code Coverage?
Code coverage is a measure of how much of a program's source code is executed when its automated tests run, usually expressed as a percentage of lines, branches or functions.
Updated November 2026 · 3 min read
Code coverage is a measure of how much of a program's source code is executed when its automated tests run, usually expressed as a percentage. If a test suite runs 800 of a program's 1,000 executable lines, line coverage is 80%. Coverage tools instrument the code, run the tests and report which lines, branches and functions were exercised and which were not.
Coverage is useful for one thing above all: finding code that no test touches. Untested code may still work, but nothing will warn anyone if a future change breaks it. What coverage cannot do is say whether the tests are any good. A test can execute a line without checking its result, so high coverage is necessary for a thorough test suite but nowhere near sufficient.
Types of Coverage
| Type | What it measures | Notes |
|---|---|---|
| Line or statement coverage | The share of executable lines or statements run by tests | The most common and easiest to understand |
| Branch coverage | The share of decision outcomes taken, such as both the true and false paths of each if statement | Stricter than line coverage. An if with no else can have full line coverage while the false path is never tested. |
| Function coverage | The share of functions or methods called at least once | Coarse but useful for spotting entirely untested code |
| Condition coverage | Whether each individual condition in a compound expression has evaluated both true and false | Catches cases branch coverage misses in expressions such as "a and b" |
| Modified condition/decision coverage (MC/DC) | Whether each condition has been shown to independently affect the decision's outcome | Required by the DO-178C standard for the most safety-critical airborne software |
Popular tools include JaCoCo for Java, coverage.py for Python, Istanbul for JavaScript and gcov for C and C++. Services such as Codecov and SonarQube track coverage over time and report it on each pull request.
How Much Coverage Is Enough?
There is no universal answer, and chasing 100% is rarely worthwhile: the last few percent tend to be error handling, generated code and trivial getters, where tests cost more than they protect. Some published guidance offers a starting point. A 2020 Google Testing Blog post on coverage best practices described 60% as "acceptable," 75% as "commendable" and 90% as "exemplary" for Google's code, while stressing that the right level depends on how critical the code is and how often it changes.
Many teams find two rules more useful than a single overall target:
- Focus on coverage of changed code. Requiring, say, 80% coverage of the lines in each new pull request improves the codebase steadily without forcing teams to backfill tests for old code that rarely changes.
- Do not let coverage fall. A check that fails when a change lowers overall coverage prevents gradual decay.
Coverage Is Not Quality
Coverage measures what tests execute, not what they check. A test that calls a function and asserts nothing adds coverage but catches nothing. When coverage becomes a target, people write such tests to hit the number, a textbook case of Goodhart's law. Mutation testing addresses this gap: a tool makes small deliberate changes to the code, such as flipping a "greater than" to "less than," and checks whether any test fails. A mutation that no test catches reveals a weak test even where coverage is 100%.
An Example
Consider a hypothetical function that applies a discount: if the customer is a member, it takes 10% off. A single test with a member customer runs every line, so line coverage is 100%. But branch coverage is only 50%, because the path where the customer is not a member never runs. Adding a second test for a non-member brings branch coverage to 100%. A mutation test then changes the 10% to 0% and the tests still pass, revealing that the member test only checked that a price was returned, not that it was correct. Fixing the assertion makes the suite meaningfully stronger without changing the coverage figure at all.
Common Pitfalls
- Mandating a high global target, which leads to low-value tests and gaming.
- Treating high coverage as proof of quality.
- Ignoring uncovered critical code because the overall number looks healthy.
- Comparing coverage across teams or codebases, which differ too much for the numbers to mean the same thing.
Coverage is often a by-product of test-driven development, is commonly reported in code review and checked in the CI/CD pipeline, and is one input among many engineering metrics.
Related terms
Test-Driven Development (TDD)
Write the test first, make it pass, then refactor.
Code Review
Peer review of code changes before they are merged.
CI/CD Pipeline
The automated path that builds, tests and deploys every code change.
Engineering Metrics
Measures of how well an engineering organization delivers and invests.