Definition
A coverage metric is a measure of how much of a chosen set of program elements a set of tests executed: the number of elements executed at least once, divided by the number of elements in the set. The elements can be source lines, functions, decision outcomes, edges between blocks of code, conditions, paths, or requirements of a specification. For elements of code, a compiler adds instrumentation §14.24, extra code that records which elements run. The figure means something only together with its element kind, its element set and its build.
The idea
A report states 100% coverage. The figure alone does not say whether every line ran, whether every decision went both ways, or whether every line of one file ran while the rest of the program was never measured. A coverage figure is a fraction, and its meaning lies in the denominator: the set of elements that could have been counted. The same tests can score 100% under one element kind and 50% under another, as the worked example shows.
A checklist is a fair picture: coverage is the share of boxes ticked, and whoever drew up the list decides what that share is worth. The picture stops at one point. A ticked box on a task list usually means the task was done right; a covered element means only that the code ran. Whether it computed the right result is a question for a test oracle §10.5, the rule that decides whether observed behavior is correct.
How it works
A coverage figure is defined by six choices.
Element kind. The common kinds are:
statement or line coverage: executable statements or source lines;
function coverage: functions entered;
branch coverage §10.20, also called decision coverage: outcomes of decisions, such as the true and false outcomes of an
if;edge coverage §10.19: transfers of control between basic blocks §3.14, the straight runs of instructions that are entered only at their first instruction;
condition coverage: the true and false values of each condition inside a compound decision such as
a && b;MC/DC (modified condition/decision coverage): every condition shown, by a pair of tests, to change the decision’s outcome on its own;
path coverage: complete routes through a function, usually too many to count.
Scope. Which source files, libraries and binaries count. Code outside the scope is outside the denominator.
Exclusions. Code removed from the denominator on purpose, such as generated code or a defensive branch that cannot run, each listed with its reason.
Instrumentation and build. The compiler adds counters to a special build. With GCC §12.17 (the GNU Compiler Collection, from the free-software project GNU, whose name stands for GNU’s Not Unix), the flag
--coverageadds the counters that the tool gcov reads. With Clang, the flags-fprofile-instr-generateand-fcoverage-mappingenable source-based coverage. The instrumented build is a different binary from the release build, and the compiler, its version, the optimization level and the other flags decide which elements exist.Collection. The tests run and the counters grow. The runtime writes the counts out when the program exits normally; a crash or a device reset before that point loses them unless the program writes them out explicitly. On a microcontroller §6.1 without a file system, the counts must be copied out through another channel, such as a debugger.
Merging and reset. Counts from several runs are merged as a union: an element counts as executed if any run executed it. The report says which runs were merged and whether the counters were reset before them.
Fuzzers report coverage in their own way. libFuzzer §10.26 prints cov:, the number of instrumented code points §12.48 reached so far, with no denominator. AFL++ §10.27, the successor of the fuzzer American Fuzzy Lop (AFL), shows a map density: the share of its coverage table in use, which is not a share of the program’s code.
Worked example
A function limits a value to at most 100:
line 1: int clamp(int x) {
line 2: int r = x;
line 3: if (x > 100)
line 4: r = 100;
line 5: return r;
line 6: }
Assume the coverage tool counts lines 2 to 5 as executable lines, and counts the decision on line 3 as having two outcomes.
Test 1 calls clamp(200). Since , lines 2, 3, 4 and 5 all run, so line coverage is . The decision came out true only, so branch coverage is . Test 2 calls clamp(7): it runs lines 2, 3 and 5 and makes the decision false. With both tests, line coverage stays at , and branch coverage becomes .
Now the scope. The program also uses a formatting library of 30 executable lines, compiled without instrumentation. The report still says 100% line coverage, and that means 4 of 4 instrumented lines. Of the program’s executable lines, the tests are known to have run 4, which is , about 11.8%. Whether they ran any of the other 30 lines is unknown, not zero.
Last, correctness. Suppose line 4 wrongly reads r = 10;. If the tests check only that clamp returns, both tests still pass, and both coverage figures stay the same. Only a test oracle that expects clamp(200) to return 100 finds the error.
Stated precisely
Let be the set of elements of the stated kind in the stated scope, after the stated exclusions, as they exist in the build that ran the tests. Let be the number of elements in , and assume . Let be the set of test runs counted, and let be the set of elements of that the instrumentation recorded as executed at least once in at least one run of . Then where is the number of elements in .
The figure is a statement about and only, and it rests on three assumptions: the instrumentation recorded every execution of every element of , with no counts lost to a crash or a reset; was taken from the same build that ran the tests; and the counts of every run in were kept. A complete report therefore states the element kind, the scope, the exclusions, the build (compiler, version and flags), the test runs, and both and as numbers, not only their ratio.
Limits and common mistakes
Coverage measures execution, not correctness. A covered line may compute a wrong value that no test checks. The coverage versus correctness §10.40 entry treats this gap.
Code that does not exist has no element. If a program lacks a needed check, no coverage metric can reveal it, because the missing check adds nothing to the denominator.
Figures with different element kinds, scopes or builds cannot be compared. Compiler optimization §3.39 can remove code, merge it, or copy a function’s body into its callers, which changes the set of elements; adding instrumentation can itself change what the optimizer does. Coverage of firmware code measured on a desktop build can differ from coverage of the same code built for the device, where other compile-time settings select other code.
Suppose 8 of 10 lines ran. Excluding one uncovered line raises coverage to 8 of 9, about 89 percent. Excluding one covered line lowers it to 7 of 9, about 78 percent. Name each exclusion and its reason so readers can reconstruct the ratio.
A coverage target invites tests that run code without checking its results. A high figure from such tests establishes that the measured elements executed. Even a failing test can contribute coverage if its counts are collected.
Questions answered
What denominator and instrumentation define the reported percentage?
The denominator is the number of elements of the chosen kind, such as executable lines, decision outcomes or edges between blocks of code, in the code that was instrumented and counted, after any declared exclusions, in the exact build that ran the tests. The instrumentation is the mechanism that recorded which of those elements executed: the compiler with its coverage flags, such as gcov counters added by the GCC compiler or Clang’s source-based coverage, or a fuzzer’s own counters. A reported percentage is defined only when the report states:
the element kind;
the scope, meaning which files, libraries and binaries were instrumented, and which were not;
the exclusions and the reason for each;
the build: compiler, version, optimization level and coverage flags;
which test runs were merged, and whether the counters were reset before them;
the numerator and the denominator as numbers.
Code outside the instrumented scope is not covered at 0%; it is not measured at all.
Related entries: Coverage-guided fuzzing (§10.12), Edge coverage (§10.19), Branch coverage (§10.20), Coverage versus correctness (§10.40), Test oracle (§10.5).