What is code coverage, and what coverage levels do safety standards require?
Short answer: Code coverage measures which parts of the code the tests executed. Statement coverage checks every line ran; branch (decision) coverage checks every if/switch took each outcome; MC/DC (Modified Condition/Decision Coverage) checks that each individual condition in a decision independently affects the outcome. Coverage shows what is not tested — it does not prove the tested code is correct. Safety standards require higher levels for more critical software: e.g. DO-178C requires MC/DC for Level A avionics software, and ISO 26262 recommends MC/DC for ASIL D.
Example: why branch coverage is not enough
if (speed > LIMIT && !override) brake();
Two tests (true/false outcome) give branch coverage, but MC/DC needs tests showing that speed > LIMIT alone changes the outcome (with override fixed) and that override alone changes it: e.g. (T,F)→brake, (F,F)→no brake, (T,T)→no brake.
Measuring
- On the host:
gcc --coverage/ gcov + lcov/gcovr reports; LLVM source-based coverage. - On the target: instrumented builds that dump counters over a debug link (gcov with semihosting or custom dump), or non-intrusive ETM trace (no code changes).
- Commercial tools: VectorCAST, LDRA, Testwell CTC++, Cantata (support MC/DC and certification evidence).
Practical targets: for ordinary product firmware, aim for high statement and branch coverage on logic modules (parsers, state machines, safety checks) rather than a single number for the whole codebase; review untested code to decide whether it needs tests, is defensive code, or is dead code.