Guide · Code coverage

Code coverage: what your tests really execute

All the tests pass. But how much of the code did they execute? Code coverage answers that question. Here is what it measures, what it does not, how it is obtained, what target to aim for and what to do with the code that remains uncovered.

Definition

What is code coverage?

Code coverage tells you how much of the source code was executed during testing. It is expressed as a percentage: if the tests execute 750 of the 1,000 statements in a program, statement coverage is 75%. It is also called test coverage, or structural coverage, because it measures the structure of the code.

The percentage is not the main point. What matters is what lies behind it: the exact list of the parts of the code that no test has ever executed. A defect there cannot have been found by the tests.

In safety-critical software, functional safety standards such as DO-178C or ISO 26262 require this measurement. Elsewhere, it is used to judge the value of a test suite and to decide where new tests are needed.

Limits

What it does not measure

Coverage tells you what the tests executed. It does not tell you what they checked.

Whether the result is right

A test that runs code without checking anything still raises coverage. The value of a test lies in its checks, not in the code it passes through.

What is missing from the code

A forgotten requirement, an error case never handled: coverage cannot see code that was never written.

Every combination

100% of statements executed does not mean that every path, every value or every sequence has been tested.

“Program testing can be used to show the presence of bugs, but never to show their absence!”

Edsger W. Dijkstra, Notes on Structured Programming, 1970

Coverage does not contradict this. It shows where the tests did not look.

Measurement

How it is measured

Most tools use instrumentation, in five steps.

  1. Instrument: at compile time, the tool adds counters to the code that record what executes.
  2. Run the tests, as they are: unit, integration or system, automated or manual.
  3. Collect the counters, on the development host or from the embedded target.
  4. Produce the report: percentages per file and per function, and the annotated source code showing what was executed, partly executed or never executed.
  5. Combine: the results of several test campaigns add up.

On an embedded target, two constraints come on top: the memory and execution time used by the instrumentation, and the means of bringing the counters back to the development host. Testwell CTC++ is designed for these constraints.

Levels

Coverage levels

Not all coverage is equal. From the weakest level to the most demanding:

LevelThe questionIts limit
FunctionHas every function been called?Says nothing about what happened inside the function.
Statement (or line)Has every statement been executed?Misses the case where a condition is false and no statement depends on it, such as an if without an else.
Decision (or branch)Has every decision been both true and false?In a compound decision such as a && b, does not show whether each condition played its part.
ConditionHas every basic condition been both true and false?The decision itself may not have taken both values.
MC/DCHas every condition been both true and false, and has it been shown to change the outcome of the decision on its own?Does not test every combination, but about N+1 cases for N conditions. This is the level required for the most critical software.
MulticonditionHave all combinations of conditions in each decision been tested?2N cases for N conditions: quickly unrealistic.

An example

A single test, price(3, 1), executes every statement of this function: statement coverage is 100%.

But the decision if (discount) has never been false: decision coverage is only 50%. The case without a discount has never been tested.

Each level in detail, test by test, on a decision with three conditions: the Coverage levels guide.

int price(int quantity, int discount)
{
    int total = quantity * 10;
    if (discount)
        total = total - 5;
    return total;
}
Test levels

At every test level

Coverage is measured at every test level, and each one tells you something different.

Unit tests

The level where high coverage is easiest to reach. It shows, function by function, the cases that were forgotten.

Integration tests

It shows what the exchanges between modules actually execute, and reveals interfaces that are never used.

System tests

It measures what the usage scenarios execute. Code not covered at this stage often points to an untested requirement, or to code that is no longer needed.

In safety-critical software, coverage is measured during requirements-based testing. Code these tests do not execute reveals an incomplete requirement, a missing test, or code that no requirement justifies.

Target

What target to aim for?

There is no universal figure. The target depends on the risk.

Outside safety-critical software

Google engineers offer guidelines: 60% is acceptable, 75% commendable, 90% exemplary. They also warn that chasing 100% at all costs gives a false sense of security.

On changed code

This is where a target is most useful: require new or modified code to be tested, with every delivery, without waiting for the existing code to catch up. The same authors consider 99% per change a reasonable goal, with 90% as a lower threshold.

Safety-critical software

The standards set the coverage level according to how critical the software is (see code coverage and standards). Each uncovered part must then be dealt with: by an additional test, or by a written justification.

A percentage on its own says little. It is better to follow its trend, look at which code remains uncovered, and match the target to the risk: a safety module deserves more than a display utility.

Uncovered code

Uncovered code

Every uncovered part has a reason. Finding it often means finding a problem.

A missing test

The most common case. Write the test.

A missing requirement

The code does something nobody asked for, or the requirement is incomplete. Complete the requirement, then the test.

Dead code

It can never execute, or no longer serves any purpose. Remove it.

Deactivated code

Intended for another configuration or another product, it must not execute here. Check that it cannot, and document it.

Defensive code

Handling of “impossible” errors, the default case of a switch. Test it by triggering these errors, for example in a unit test, or justify it.

In a project subject to a standard, whatever remains uncovered must be justified in writing, and these justifications are part of the file handed to the auditor. A good tool lets you attach them to the code, so that they carry over into the following reports.

Tools

Choosing a tool

Coverage tools differ widely. Eight points to check:

  1. The levels measured: up to MC/DC if your standard requires it, now or later. Many tools stop at decisions.
  2. Compiler independence: the tool follows all your projects, and survives a change of compiler.
  3. Instrumentation overhead: in memory and execution time, decisive on an embedded target.
  4. Languages: those of your current projects, and those you plan to use.
  5. Integration: command line, build chain, continuous integration, without changing the tests.
  6. Reports: see at a glance what is missing, down to the condition, and export the results.
  7. Qualification: for safety-critical software, a tool certificate or a qualification kit reduces the work.
  8. The vendor: an evaluation on your own projects, responsive support, references in your industry.

These points are drawn from the Verifysoft Technology GmbH white paper Measuring Code Coverage: 10 Criteria for selecting a Code Coverage Tool ↗. The compiler sometimes comes with its own tool: see Testwell CTC++ or gcov?

Testwell CTC++

Measuring coverage with Testwell CTC++

Testwell CTC++ measures the coverage of C and C++ code (Java optional), from function level up to MC/DC and multicondition:

  • on the host as well as on the embedded target, with low instrumentation overhead;
  • whatever the compiler, cross-compilers included;
  • without changing the tests or the continuous integration;
  • with a report that shows uncovered code, condition by condition, and the justifications for uncovered parts;
  • certified by TÜV SÜD for IEC 61508, ISO 26262, EN 50716, ISO 25119 and IEC 62304.

Testwell CTC++ →
Certification →
Testwell CTC++ resources →

Prove what your tests cover1 min 21 s · silent animation
TiCS

Tracking coverage with TiCS

TiCS takes in the coverage measured at each test level, for example by Testwell CTC++:

  • coverage is shown per test level, and in total;
  • it feeds into the TQI, alongside the other code quality indicators;
  • its evolution can be followed from one version to the next.

TiCS →
Guide: technical debt →

Coverage, level by level1 min 36 s · silent animation

Key points

  • Code coverage measures what the tests executed, not what they checked.
  • Its main contribution is the list of code that no test has executed.
  • The levels range from function to MC/DC and multicondition; 100% of statements does not mean 100% of decisions.
  • There is no universal target: set it according to the risk, starting with changed code.
  • Every uncovered part is dealt with: a test, a removal, or a written justification.
FAQ

Frequently asked questions

Are code coverage and test coverage the same thing?

Most of the time, yes. Some keep “test coverage” for the share of requirements verified by tests. Code coverage, for its part, concerns the structure of the code.

Should you aim for 100%?

For safety-critical software, the standard requires everything uncovered to be dealt with, by a test or a justification. Elsewhere, the last few percent cost a lot and bring little: it is better to cover risky code and changed code well.

Does measuring coverage slow the tests down?

Instrumentation adds code and execution time. The overhead varies widely from one tool to another. It matters most on embedded targets and for real-time tests.

Line coverage or statement coverage?

A line can contain several statements, and a statement can span several lines. Many tools count lines; the standards speak of statements.

Can you measure the coverage of manual tests?

Yes. Instrumented code records what executes, however the program is started.

How do I know whether my tests really check anything?

Coverage does not tell you. Test reviews do, and so does mutation testing: the code is deliberately modified to see whether a test fails.

Measure what your tests really cover

A free trial of Testwell CTC++ on your own C or C++ code, on the host or on the embedded target.

Free trial →

Testwell CTC++  ·  Certification  ·  Testwell CTC++ or gcov?  ·  Static code analysis  ·  Standards  ·  Learn

Sources: Carlos Arguelles, Marko Ivanković and Adam Bender, Code Coverage Best Practices (Google Testing Blog, 2020) ↗; Edsger W. Dijkstra, Notes on Structured Programming (EWD249, 1970); Verifysoft Technology GmbH, Measuring Code Coverage: 10 Criteria for selecting a Code Coverage Tool ↗.