Decision coverage

Decision coverage is a white box coverage measure: the share of decision outcomes in the code that a test suite has exercised, where each decision, such as an if or a loop condition, has a true outcome and a false one. In everyday use the name is interchangeable with branch coverage, although ISTQB defines the two differently.

The longer version

A decision is a point where the code chooses which way to go: an if, the condition of a loop, the selector of a switch. A two-way decision has two outcomes, true and false, and decision coverage counts how many of those outcomes the tests have produced. The ISTQB glossary's definition is the coverage of decision outcomes. Take a function that works out a library fine. It starts the fine at zero and then makes three decisions, each an if with no else: if the book is overdue, charge for each day; if the borrower is a child, halve the fine; if the fine is above the cap, reduce it to the cap. Three decisions give 3 × 2 = 6 outcomes. One test, an adult returning a book four days late, makes the first decision true and the other two false, so 3 of the 6 outcomes are exercised and decision coverage is 3 ÷ 6, or 50 per cent. A second test, a child returning a book so late that the cap applies, adds the true outcomes of the second and third decisions and brings the figure to 5 ÷ 6, about 83 per cent. The outcome still missing is the first decision being false: nobody has tested a book returned on time.

Statement coverage is the weaker neighbour, and the same function shows why. Run only the second test, the child with the long-overdue book. All three conditions are true, every statement in the function executes, and statement coverage is complete, yet only the three true outcomes have been taken and decision coverage stands at 3 ÷ 6. The false side of an if with no else contains no statement, so a measure that counts statements cannot see whether it was ever taken. Decision coverage asks for it explicitly. Within a function that has decisions, taking every one both ways also runs every statement that can be reached, which is why this is called the stronger of the two measures. It is not the strongest. A decision made of several conditions, such as overdue and not exempt, can be made true and false without each of its conditions having been both true and false, which is what condition coverage examines. Taking each decision both ways also says nothing about combinations of decisions, and that is the territory of path testing.

Whether decision coverage and branch coverage are one measure depends on who is asked. In ordinary use the two names label the same requirement, each decision taken both ways. ISTQB keeps them apart. Its glossary defines branch coverage as the coverage of branches in a control flow graph, and the Foundation Level syllabus, version 4.0.1, describes a branch as a transfer of control between two nodes of that graph, which can be unconditional, meaning straight-line code, or conditional, meaning a decision outcome. Branch coverage in that sense counts the conditional branches that decision coverage counts and the unconditional ones as well. The syllabus states that branch coverage subsumes statement coverage: any set of test cases achieving full branch coverage also achieves full statement coverage, but not the other way round. Its release notes give three reasons for replacing decision testing with branch testing: branch coverage is more commonly used in practice, different standards define the decision differently, and the earlier claim that full decision coverage implies full statement coverage is not true of a program with no decisions. Either figure, like any coverage figure, shows what ran and not whether anything was checked.

Two stronger criteria look inside compound decisions. Condition coverage asks that every condition in a decision has taken each possible outcome, and it can be met without decision coverage. Suppose the fine is waived if the borrower is a child or the book was reserved for a class. One test with a child and no reservation and a second with an adult and a reservation make each condition both true and false, yet the decision comes out true both times and its false outcome is never taken. Modified condition/decision coverage, or MC/DC, asks in addition that each condition be shown to affect the decision's outcome on its own, by varying that condition while the others are held fixed. Wikipedia records that the avionics guidance DO-178B and DO-178C uses MC/DC for the most critical software, Level A, and that IEC 61508 and ISO 26262 recommend or require it in certain cases. The standards also differ over the plain measure. Rapita Systems points out that decision coverage is the term DO-178C uses while ISO 26262 refers to branch coverage, that the DO-178C definition counts the assignment of a Boolean variable, such as a = b || (c && d), as a decision, and that a switch with three of its four branches taken scores 75 per cent as branch coverage but leaves the decision uncovered.

Common questions

What is decision coverage?

Decision coverage is the proportion of decision outcomes that a set of tests has exercised. A decision is a point where code chooses between routes, such as an if or a loop condition, and its outcomes are the routes it can choose. Full decision coverage means every decision has been seen to go each way at least once. The ISTQB glossary defines it as the coverage of decision outcomes.

How is decision coverage calculated?

Divide the number of decision outcomes the tests exercised by the total number of decision outcomes, and express the result as a percentage. A function with four two-way decisions has 4 × 2 = 8 outcomes. If the tests take six of them, decision coverage is 6 ÷ 8, or 75 per cent, and the two untaken outcomes are the next tests to write.

Is decision coverage the same as branch coverage?

Everyday usage treats the two names as one measure. ISTQB separates them: its glossary defines decision coverage as the coverage of decision outcomes and branch coverage as the coverage of branches in a control flow graph, and its Foundation Level syllabus counts unconditional transfers of control as branches too. Version 4.0.1 of that syllabus teaches branch testing in place of decision testing.

What is the difference between decision coverage and statement coverage?

Statement coverage counts the statements that ran, and decision coverage counts the decision outcomes that were taken. An if with no else can be fully covered as statements by one test in which the condition is true, while its false outcome goes untested. Decision coverage requires a second test for that outcome, so for code containing decisions it is the stricter of the two measures.

What is the difference between condition coverage and decision coverage?

Decision coverage looks at the overall result of each decision, true and false. Condition coverage looks at each condition inside it, so in 'child or reserved' both 'child' and 'reserved' must each be true in one test and false in another. Neither implies the other: two tests can satisfy condition coverage while the whole decision is true both times. Condition/decision coverage requires both at once, and MC/DC goes further still.

What is MC/DC coverage?

Modified condition/decision coverage requires every decision to take each outcome, every condition to take each outcome, and each condition to be shown to change the decision's result independently, by varying it alone while the other conditions stay fixed. Wikipedia notes that DO-178B and DO-178C use it for Level A, the most critical class of airborne software, and that IEC 61508 and ISO 26262 recommend or require it in certain cases.

What does full decision coverage leave untested?

Full decision coverage leaves three things open. Combinations of decisions are not required, so a defect that needs two particular outcomes together can survive. The separate conditions inside a compound decision need not each have affected the result. And nothing in the measure asks whether the tests asserted anything, so every outcome can be taken by tests that check no result.

Bug reports that arrive ready to triage.

Collect replay, console, network and browser context with the Free plan.

<script src="…/widget.js" data-key="pb_pub_…">