Line Coverage

Definition of Line Coverage Metric

The Line Coverage of a program is the number of executed lines divided by the total number of lines. Only lines that contain executable statements are considered, not those with pure declarations.

Advantage

Line Coverage is very suitable for both communicating and visualizing a coverage overview. A developer’s statement “I have 80% of my software’s lines covered through unit tests.” is easy to understand for the human recipient. Especially if it goes along with a view of the source code that has the lines colored in green and red, respectively.

The simplicity that is good for conveying a first impression has its drawbacks in terms of precision however. See the following sections for details.

Formatting Dependency

Line coverage is an unstable coverage measure since it depends strongly on the code formatting. Two variations of a simple C function will serve  as an example:

Variant 1: Concise version with two lines of executable code:

double kelvin_to_celcius(double kelvin)
{
    if (kelvin < 0.0) return -1;
    return kelvin - 273.15;
}

Variant 2: Reformatted version that got expanded to three lines of executable code:

double kelvin_to_celcius(double kelvin)
{
    if (kelvin < 0.0)
        return -1;
    return kelvin - 273.15;
}

A simple change of the coding style leads to a coverage change from 50% to 66.7% for a call like

kelvin_to_celcius(-10.0);

that tests the error handling!

Disguised Control Flow

Let’s assume an invocation of Variant 1 with a succesful conversion:

kelvin_to_celcius(250.0);

Both lines will be executed thus resulting in 100% coverage. Have we completed our testing? Nay! The error case needs as a test as well. Alas, the possible branching of the code flow is not part of what Line Coverage cares about.

Relevance for Safety Standards

None.  Due to the limitations described above Line Coverage does not provide any value over levels like Branch Coverage or – more importantly – MC/DC. Common standards like ISO 26262 for functional safety therefore speak out recommendations for the levels like the latter.

Alternatives to Line Coverage

The formatting dependency is remediated by Statement Coverage. One needs to switch to Branch Coverage to address execution flow branching. If one also cares about logic expressed through boolean expressions (leading to branching) we recommend to measure a Condition Coverage metric like MC/DC.

Platforms-Compilers-750x750