Definition
Coverage is a measure of how much of a program a set of tests executed, such as the fraction of branch outcomes that the tests produced. Correctness is the property that a program meets its specification: the statement of which output or effect each input must produce. So coverage records which code ran, and correctness concerns whether the results were right. A test supports correctness only for the inputs it ran, and only where a test oracle §10.5, the check that decides pass or fail, compared the result with the specification.
Where it fits
Main entry: Coverage metric (§10.18).
The coverage metric entry explains how coverage is counted. Coverage cannot stand in for correctness, even at 100%, because a test reveals a fault, a defect in the code, only when four things happen. The test executes the faulty code. The faulty code produces a wrong value for that test’s input. The wrong value reaches an output that the test observes. The test oracle compares that output with the specification. Testing textbooks call the first three conditions reachability, infection and propagation. Coverage measures only the first.
A fault can therefore survive complete coverage in two ways. The oracle may be weak: it checks only that the program did not crash, or it checks a property that the wrong result also has. Or the faulty code may give the right output for the particular inputs tested, which is called coincidental correctness. Coverage also cannot reveal missing code: a check that the program never makes adds no element to count.
Coverage is informative in the other direction. Code that no test executed has no test evidence at all, so a low figure marks untested code. To measure whether the tests would notice a wrong result, mutation testing §10.45 plants small faults in copies of the program and counts how many of them the tests detect.
Questions answered
Can all branches execute while one arithmetic result is always wrong?
Yes, every branch can execute while one arithmetic result is wrong for every input. Branch coverage §10.20, the fraction of decision outcomes that the tests produced, records only that each branch ran, not that any test compared the branch’s result with the required one. Take a function specified to return 0 for an integer and otherwise, and implemented as if (x < 0) return 0; else return x + 1;. The second branch is wrong for every , because for every .
The two tests and produce both outcomes of the decision, so branch coverage is , which is 100%. If the tests check only that the function returns, both pass, and the wrong value is never compared with the required . A test that checks the exact result for fails, at the same coverage. Even an exact check misses a fault when the chosen input hides it: had the second branch returned x * 2, the test would pass, because . Complete coverage is a statement about which code ran; correctness is a statement about the result for every input.
Related entries: Test oracle (§10.5), Edge coverage (§10.19), Branch coverage (§10.20), Coverage metric (§10.18), Mutation testing (§10.45).