Statement Coverage

Statement Coverage

Statement Coverage Metric Definition

Statement coverage tracks the executed program statements. The metric value of a program is the number of executed statements in it, divided by the total number of statements.

Statement Blocks

A block groups a sequence of statements and treats them like a single statement. In the C-family of languages curly braces ({ and }) are used for grouping. A basic block is a statement block with no jump instructions in it. Given a single entry and a single exit point only Statement Coverage can be simplified by basing calculations on complete blocks.

Advantage

Unlike Line Coverage this metric is robust against plain code formatting changes. After all, many programming languages allow for multiple statements in a single line. Splitting of lines should not influence the coverage value.

Relation to other metrics

100% Statement Coverage implies 100% Line Coverage. But the opposite is not true: a line may contain multiple statements whereas a test may not succeed in executing all of them. The following example shows a line with two statements where the latter one is skipped if the variable done  is false from the start.
while (!done) { readMoreData(); }

Shortcomings

A plain count of executed statements does not take possible branching points and input conditions for branching decisions into consideration.
if (config->loggingEnabled())
    log("Will start to process data");
startProcessing();
A single test will be able to execute all of the above three statements. Thus resulting in 100% coverage at this level. But is it complete? The case of loggingEnabled() returning a false boolean value requires a second test.  To exercise the else branch that would become visible when transforming this code into a flow diagram.

Relevance for Safety Standards

ISO 26262 highly recommends Statement Coverage for ASIL A and B. ASIL C and D recommend it as well but the more strict Branch Coverage and Modified Condition/Decision Coverage are highly recommended instead. EN 50128 recommends this metric for SIL 0. IEC 61508 recommends this metric for SIL 1 and highly recommends it for SIL 2, 3 and 4.