Code Coverage

What does coverage actually mean? There are different categories.

Statement Coverage

Statement coverage is achieved when all statements in a method have been executed at least once.

Faults cannot be discovered if the parts containing them are not executed Equivalent to covering all nodes in control flow graph (actual % of coverage would be different) Executing a statement is a weak guarantee of correctness, but easy to achieve In general, several inputs execute the same statements – important question in practice is how can we minimize test cases?

  • Statement coverage is most used in industry
  • Typical coverage target is 80-90% (not 100% because there is dead code, and no need to test on getter and setter methods)

Branch Coverage

Branch coverage is achieved when every branch from a node is executed at least once

  • At least one true and one false evaluation for each predicate
  • Can be achieved with D+1 paths in a control flow graph with D 2-way branching nodes and no loops
    • Even less if there are loops

Segment (Basic Block) Coverage

Segment coverage counts basic blocks (maximal straight-line code sequences) rather than individual statements.

Condition Coverage

Condition coverage reports the true or false outcome of each condition. Condition coverage measures the conditions independently of each other.

Condition coverage seems to exclude the else condition. If instead you have condition/decision coverage, you also need to look at the branching.

There is a short-circuit problem: in a || b, if a is true, b is never evaluated — so pure condition coverage can miss cases.

Modified Condition/Decision Coverage (MCDC)

Each condition should be evaluated one time to true and one time to false, and each condition should independently affect the decision’s outcome.

Key idea: test important combinations of conditions while limiting testing costs. Often required for mission-critical systems.

Example for (a || b) && c:

Path Coverage

Path coverage requires every possible path through the control flow graph to be executed.

Similar to branch coverage but considers full paths end-to-end rather than each branch independently.

Loop Coverage

Loop coverage requires loops to be exercised across their boundary cases.

At minimum, execute the loop zero times, once, and twice-or-more times. Broader coverage uses: minimum ± 1, minimum, typical, maximum, maximum ± 1.