Guide · Code coverage

Coverage levels: from statements to MC/DC

Function, statement, decision, condition, MC/DC, multiple condition: each level asks a more demanding question than the one before. This guide compares them on the same C example, test by test, to show what each one guarantees and what it lets through.

Vocabulary

Decision and condition

Coverage levels rest on two words that need to be told apart.

A condition is an elementary Boolean expression, with no logical operator: temp > 90, or simply manual.

A decision is a Boolean expression made of one or more conditions joined by logical operators (&&, ||, !): (temp > 90 && pressure > 5) || manual. It chooses the program's path, in an if, a while or a for.

When a decision contains only one condition, all the levels come together: testing it true, then false, is enough. The differences between levels appear with compound decisions.

Example

The example

A monitoring function raises an alarm when both temperature and pressure are too high, or when the operator requests it.

Its decision contains three conditions:

  • A: temp > 90;
  • B: pressure > 5;
  • C: manual.

The table gives the five tests used in the rest of this guide.

int alarm(int temp, int pressure, int manual)
{
    int level = 0;
    if ((temp > 90 && pressure > 5) || manual)
        level = 2;
    return level;
}
TestCallABCDecision
T1alarm(95, 6, 0)TT–true
T2alarm(80, 6, 0)F–Ffalse
T3alarm(95, 4, 0)TFFfalse
T4alarm(95, 4, 1)TFTtrue
T5alarm(80, 6, 1)F–Ttrue

T: true. F: false. –: condition not evaluated, because of short-circuit evaluation.

Level by level

What each level requires

For each level: the smallest set of tests that reaches 100% on the alarm function, and what it lets through.

1 test

Function

Has each function been called?

Any single call is enough. This level finds the functions that were never called, and nothing more.

1 testT1

Statement

Has each statement been executed?

T1 makes the decision true and executes level = 2: every statement is executed.

What gets through: the case where the alarm is not raised has never been tested.

2 testsT1, T2

Decision (or branch)

Has each decision been true, then false?

T1 makes the decision true, T2 makes it false. This level includes the previous one: when every decision takes both values, every statement is executed.

What gets through: B has never been false, nor C true. The role of the pressure and that of the manual request have not been tested.

3 testsT1, T2, T4

Condition

Has each condition been true, then false?

A is true in T1 and false in T2; B is true in T1 and false in T4; C is false in T2 and true in T4.

What gets through: nothing shows that the pressure changes the outcome. If the code ignored the pressure, these three tests would still pass. And in general this level does not even include decision coverage, which is why it is often combined with it, as condition/decision coverage.

4 testsT1 to T4

MC/DC

Has each condition been shown to change the outcome of the decision on its own?

For each condition, you need two tests in which only that condition changes value, and the decision changes too: A with T1 and T2, B with T1 and T3, C with T3 and T4. Four tests are enough, that is N + 1 for N conditions. MC/DC stands for Modified Condition/Decision Coverage. The method, the variants and the pitfalls: the MC/DC guide.

What changes: T3 raises an alarm if the code ignores the pressure. The defect that escaped the previous levels is detected.

5 testsT1 to T5

Multiple condition

Have all combinations of conditions been tested?

Three conditions give 23 = 8 combinations. In C, because of short-circuit evaluation, only five can occur: T1 to T5.

Its cost: it is the most thorough level, but the number of cases doubles with each added condition.

C and C++

Short-circuit evaluation in C and C++

In C and C++, a decision is not always evaluated in full.

The && and || operators stop as soon as the result is known. If the temperature is low, the pressure is not read: A && B is false whatever happens. If A && B is true, manual is not read: the decision is true.

Conditions not evaluated

In T1, C is not evaluated; in T2 and T5, B is not. A condition that is not evaluated is neither true nor false: it does not count for condition coverage.

Fewer combinations

Of the 8 combinations of A, B and C, only 5 can be told apart at run time. Multiple condition coverage is measured on these 5 cases.

MC/DC pairs to check

In MC/DC pairs, the other conditions must keep their value. Between T1 and T2, B is not evaluated in T2, but the pressure passed to the function stays the same. The variants of MC/DC differ in how they handle this case.

For writing tests, short-circuit evaluation changes little. For measuring, it matters: two tools that handle it differently give different percentages on the same code.

Comparison

The levels side by side

From the weakest level to the most demanding, on the alarm function:

LevelTestsWhat it adds
Function1Finds the functions never called.
Statement1Finds the code never executed.
Decision2Also tests the case where the decision is false, even when no statement depends on it.
Condition3Tests each condition with both values, without showing its effect on the outcome.
MC/DC4 (N + 1)Shows that each condition changes the outcome on its own.
Multiple condition5 (8 without short-circuit)Tests every combination: up to 2N cases for N conditions.

Each level includes the previous one, except condition coverage: 100% decision coverage implies 100% statement coverage, and MC/DC implies both decision and condition coverage. With ten conditions, MC/DC requires at least 11 tests, multiple condition coverage up to 1,024.

Other measures

Other measures

Other measures exist. Some are common in tools, others are mostly theoretical.

Line

The most common in general-purpose tools. It is close to statement coverage, but one line can hold several statements, or even a whole decision.

Call

Has each function call been executed, at every place where it appears? It is used in integration testing: ISO 26262 lists it, with function coverage, at the software architecture level.

Loop

Has each loop been run zero times, once and several times? It targets boundary errors.

Path

Has every possible path through the function been taken? With loops, the number of paths quickly becomes infinite: this measure remains mostly theoretical.

Object code

For the most critical airborne software, DO-178C also requires analysing the code added by the compiler that is not directly traceable to the source code.

Choice

Which level should you choose?

The level depends on what a failure would cost.

Software under a standard

The standard sets the level according to criticality. DO-178C, for example, requires statement coverage at level C, adds decision coverage at level B and MC/DC at level A. The code coverage and standards guide compares the other standards.

Outside standards

Decision coverage is a good starting point: it catches the "else" cases that statement coverage lets through, for a moderate testing effort.

Code at risk

For a complex decision in a sensitive module, MC/DC checks that each condition matters, for a few more tests.

The level can also vary from one part of the code to another: nothing prevents you from requiring MC/DC on a safety module and decision coverage on the rest.

Testwell CTC++

Every level with Testwell CTC++

Testwell CTC++ measures every level presented in this guide on C and C++ code (Java as an option): function, statement, decision, condition, MC/DC and multiple condition.

  • The report shows, decision by decision, the conditions and combinations that have not been tested;
  • measurement works on the host as well as on the embedded target, whatever the compiler;
  • the tool is certified by TÜV SÜD for IEC 61508, ISO 26262, EN 50716, ISO 25119 and IEC 62304.

Testwell CTC++ →
Certification →
Testwell CTC++ or gcov? →

All coverage levels1 min 41 s · silent animation
TiCS

Tracking coverage with TiCS

TiCS takes in the coverage measured by Testwell CTC++ or by another coverage tool:

  • coverage is shown per test level, and in total;
  • it feeds into the TQI, alongside the other code quality indicators;
  • its evolution can be followed from one version to the next.

TiCS →
Guide: code coverage →

Coverage, level by level1 min 36 s · silent animation

Key points

  • A condition is an elementary Boolean expression; a decision combines one or more of them.
  • 100% statement coverage does not guarantee that each decision has been false; 100% condition coverage does not guarantee that each condition matters.
  • MC/DC shows that each condition changes the outcome on its own, with at least N + 1 tests for N conditions.
  • In C and C++, short-circuit evaluation reduces the number of combinations and changes how they are counted.
  • The level is chosen according to risk: the standard sets it for critical software, and decision coverage is a good starting point elsewhere.
FAQ

Frequently asked questions

Are branch coverage and decision coverage the same thing?

Almost. For an if, the two branches match the two values of the decision. The differences lie in the details: the cases of a switch, or what the tool calls a decision. DO-178C, for example, calls any Boolean expression a decision, even outside an if, as in ok = a && b;

How many tests does MC/DC require?

For a decision with N conditions, between N + 1 and 2N tests. That is far fewer than the 2N combinations of multiple condition coverage: at least 11 tests instead of 1,024 for ten conditions.

Is MC/DC only for aerospace?

No. Born in aerospace, it is also recommended by ISO 26262 for the most critical automotive functions, and listed by other functional safety standards, such as IEC 61508.

Why do two tools give different percentages?

Because they do not count the same thing: lines or statements, decisions with or without the cases of a switch, conditions with or without short-circuit evaluation. See Testwell CTC++ or gcov?

Is a while loop a decision?

Yes. Its condition must be true, to enter the loop, and false, to leave it. Loop coverage goes further: zero, one and several iterations.

Should you aim for 100% MC/DC everywhere?

No. This level has a cost: it is kept for code whose failure would have the most serious consequences, or for code the standard designates.

Measure every level, up to MC/DC

A free trial of Testwell CTC++ on your own C or C++ code, on the host or on the embedded target.

Free trial →

Code coverage  ·  Testwell CTC++  ·  Certification  ·  DO-178C  ·  Standards  ·  Learn

Source: Kelly J. Hayhurst, Dan S. Veerhusen, John J. Chilenski and Leanna K. Rierson, A Practical Tutorial on Modified Condition/Decision Coverage (NASA/TM-2001-210876, 2001) ↗.