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.
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.
Coverage tells you what the tests executed. It does not tell you what they checked.
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.
A forgotten requirement, an error case never handled: coverage cannot see code that was never written.
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!”
Coverage does not contradict this. It shows where the tests did not look.
Most tools use instrumentation, in five steps.
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.
Not all coverage is equal. From the weakest level to the most demanding:
| Level | The question | Its limit |
|---|---|---|
| Function | Has 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. |
| Condition | Has every basic condition been both true and false? | The decision itself may not have taken both values. |
| MC/DC | Has 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. |
| Multicondition | Have all combinations of conditions in each decision been tested? | 2N cases for N conditions: quickly unrealistic. |
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;
}
Coverage is measured at every test level, and each one tells you something different.
The level where high coverage is easiest to reach. It shows, function by function, the cases that were forgotten.
It shows what the exchanges between modules actually execute, and reveals interfaces that are never used.
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.
There is no universal figure. The target depends on the risk.
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.
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.
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.
Every uncovered part has a reason. Finding it often means finding a problem.
The most common case. Write the test.
The code does something nobody asked for, or the requirement is incomplete. Complete the requirement, then the test.
It can never execute, or no longer serves any purpose. Remove it.
Intended for another configuration or another product, it must not execute here. Check that it cannot, and document it.
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.
Coverage tools differ widely. Eight points to check:
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++ measures the coverage of C and C++ code (Java optional), from function level up to MC/DC and multicondition:
TiCS takes in the coverage measured at each test level, for example by Testwell CTC++:
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.
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.
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.
A line can contain several statements, and a statement can span several lines. Many tools count lines; the standards speak of statements.
Yes. Instrumented code records what executes, however the program is started.
Coverage does not tell you. Test reviews do, and so does mutation testing: the code is deliberately modified to see whether a test fails.
A free trial of Testwell CTC++ on your own C or C++ code, on the host or on the embedded target.
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 ↗.