Code Coverage Analysis

Statistics
Introduced by
Test Coverage Community
Year
1988
Type
measurement and analysis
Family
Test measurement

What does it do?

Code coverage analysis measures which parts of a program's source code are executed when a test suite runs, usually at the level of statements, branches, or methods.

Measurement tools instrument the code so that execution can be recorded, and the results are reported as coverage percentages together with the locations of unexecuted code. The method is used to judge the breadth of a test suite and to locate regions that no test reaches.

The intuition behind the method

A test can only reveal a fault in code that it runs. Coverage records which parts of the code the tests executed, so untested regions become visible. A low figure marks places where faults could remain undetected. A high figure shows only that code was executed, not that its behaviour was checked, so coverage describes reach rather than correctness.

History

Zhu, Hall, and May (1997) survey test adequacy criteria, reviewing their rationale, their classification, and methods for comparing and assessing them. Frankl and Weiss (1988) report an experimental comparison of the effectiveness of branch testing and data flow testing. Corbet (2008) describes code coverage applied to the Linux kernel. Many coverage tools exist for different languages and build environments.

When to use it

Use coverage analysis to find code that no test reaches, to track test breadth across releases, and to check whether a changed module has its branches exercised. It fits continuous integration, where a report can show when coverage drops after a change. Do not use a single coverage percentage as the criterion for release readiness or as a measure of a developer's performance. Coverage says nothing about whether assertions check the right outcome, so it should be read alongside review of test design. Regulated or safety-critical projects may require coverage evidence, but the required criterion depends on the applicable standard.

Limitations and considerations

  • Coverage records execution, not checking: a test that runs a line but asserts nothing still counts toward coverage.
  • The criteria differ in what they reveal; line coverage can be complete while some branch outcomes are never taken.
  • Path coverage grows rapidly with the number of conditional branches, so exhaustive path coverage is generally infeasible for real programs.
  • Instrumentation alters the program being measured, and exclusion rules for third-party or generated code change the reported figure.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Gives an objective count of which program elements a test suite executes, without judging the quality of the tests.
  • Lists uncovered elements precisely, so the report can direct where new tests or review are needed.
  • Can run automatically on every change, so a drop in coverage is visible early.
  • Reports several criteria (statement, branch, method) together, showing whether conditions as well as lines are exercised.

Applications

  • Finding code that no test in the suite reaches, to decide whether it needs tests or review.
  • Comparing coverage between releases in a continuous integration pipeline, to see whether a change left new code untested.
  • Directing test writing to modules where branch outcomes are not exercised.
  • Producing coverage evidence for an audit or regulatory review where the applicable standard asks for it.

How it works

How it works

1

Collect the system under test, its test suite, and the build configuration needed to run both. Decide which coverage criteria (for example statements or branches) will be reported and which files are in scope.

2

Instrument the code by inserting probes into the source or by rewriting the compiled form, so that each statement or branch records when it runs. This is the step where probes are added; the acquisition step only gathers inputs.

3

Run the test suite against the instrumented program and record which probes fire. The result is a set of covered and uncovered program elements for each test run.

4

Count the covered elements against the total for each criterion and compute the corresponding percentage. Statement coverage is the number of executed statements divided by the total number of statements, multiplied by 100. Branch coverage is the number of decision outcomes exercised (the true and the false outcome of each conditional) divided by the total number of decision outcomes, multiplied by 100. Method coverage is the number of executed methods divided by the total number of methods, multiplied by 100. Report each criterion separately, and list the uncovered elements by file and region so they can be inspected.

5

Review each uncovered region to decide whether it reflects a missing test, unreachable code, or code excluded on purpose. Unexecuted code is not necessarily dead code; the reason it is not executed has to be judged from the code and its requirements. Read the resulting percentage together with its criterion, its denominator, and the exclusion rules applied. A figure reflects breadth relative to the chosen criterion only, and figures produced by different tools or configurations are not directly comparable.

Common pitfalls

  • Setting a fixed percentage target and writing tests to meet it can produce tests that execute code without meaningful checks.
  • Treating uncovered code as dead code, or as untested risk, without examining why it is uncovered.
  • Reading a high percentage as evidence of correctness; passing tests on covered code do not rule out logic defects.
  • Excluding third-party or generated code from the report changes the percentage, so the exclusion rules must be stated with any reported figure.

Frequently asked questions

What is the difference between statement, branch, and path coverage?

Statement coverage measures whether each statement executes at least once. Branch coverage requires that each conditional (such as an if or switch) is evaluated both true and false. Path coverage requires that all distinct execution sequences run. Path coverage is the strongest of the three but is often infeasible; branch coverage is a common practical compromise.

Is 100% coverage a realistic or desirable goal?

Full coverage is often impractical. The last uncovered elements are frequently error handlers, rarely used configuration, or code that is hard to trigger from tests. Whether the remaining effort is justified depends on the risk of those regions. No universal target percentage applies across projects; a team should set its target from its own risk assessment.

How does high coverage coexist with high defect rates in some projects?

Coverage measures breadth (code executed), not depth (the quality of assertions). A test can cover a line without asserting the correct outcome, so high coverage with weak assertions can give false confidence. Meaningful test design should accompany any coverage goal.

How do I address coverage gaps in third-party libraries or generated code?

Exclude third-party and generated code from the report using the configuration rules of the coverage tool, and state the exclusions. Some teams report generated code separately from handwritten code rather than applying one threshold to both; whichever choice is made should be documented.

Related methods

Sources

  1. Zhu, H., Hall, P. A. V., & May, J. H. R. (1997). Software unit test coverage and adequacy. ACM Computing Surveys, 29(4), 366–427.

    Open DOI reference
  2. Frankl, P. G., & Weiss, S. N. (1988). An experimental comparison of the effectiveness of branch testing and data flow testing. IEEE Transactions on Software Engineering, 14(12), 1763–1773.

    Open publication reference
  3. Corbet, J. (2008). Code coverage for the Linux kernel. Linux Weekly News article.

    Open publication reference