A difference between statement and decision coverage

Statement coverage is said to make sure that every statement in the code is executed at least once.
Decision/branch coverage is said to test that each branch/output of a decisions is tested, i.e. all statements in both false/true branches will be executed.
But is it not the same? In Statement coverage I need to execute all statements so I guess it can be only done by running all possible ways. I know I am missing something here..

11.5k 26 26 gold badges 107 107 silver badges 175 175 bronze badges asked Jan 25, 2013 at 10:00 5,015 15 15 gold badges 42 42 silver badges 67 67 bronze badges

I guess this question is better for Programmers.StackExchange.com. By the way decision and branch coverage are not the same thing!

Commented Jan 25, 2013 at 10:03 @Adriano Yes, wiki says so but a lot of other sources say it is the same. Commented Jan 25, 2013 at 10:09

Not just wiki, formal definition for "decision" is each condition to enter the code path of a branch (then branch is the whole condition). Imagine, for example, the short circuit in C/C++. You may decide to use them as synonyms or not.

Commented Jan 25, 2013 at 10:15

Moreover a statement may not be executed even if the branch where it is has been executed because of, for example, jumps, exceptions and/or other asynchronous conditions (locks, signals, events). That's why decision coverage and statement coverage may differ.

Commented Jan 25, 2013 at 10:18

5 Answers 5

The answer by Paul isn't quite right, at least I think so (according to ISTQB's definitions). There's quite a significant difference between statement, decision/branch, and condition coverage. I'll use the sample from the other answer but modify it a bit, so I can show all three test coverage examples. Tests written here give 100% test coverage for each type.

if (a || b) < test1 = true; >else < if (c) < test2 = true; >> 

We have two statements here - if(a||b) and if(c), to fully explain those coverage differences:

  1. statement coverage has to test each statement at least once, so we need just two tests:

This way we executed each and every statement.

  1. branch/decision coverage needs one more test:

That way we have all the branches tested, meaning we went through all the paths. 3. condition coverage needs another test:

That way we have all conditions tested, meaning that we went through all paths (branches) and triggered it with each condition we could - the first 'if' statement was true in the first test because a=true triggered it and in the last test because b=true triggered it. Of course, someone can argue that case with a=true and b=true should be tested as well, but when we check how 'or' works then we can see it isn't needed and also variable c can be of any value as in those tests it is not evaluated.

At least I interpreted it this way. If someone is still interested :)

EDIT: In most sources, I found lately decision/branch coverage terms are equivalent, and the term I described as decision coverage is in fact condition coverage hence that update of the answer.