Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 848
John V Avatar asked Jan 25 '13 10:01

John V


People also ask

What is difference between decision coverage and branch coverage?

Decision coverage measures the coverage of conditional branches; branch coverage measures the coverage of both conditional and unconditional branches. The Syllabus uses decision coverage, as it is the source of the branches.

What does decision coverage mean?

Decision coverage or Branch coverage is a testing method, which aims to ensure that each one of the possible branch from each decision point is executed at least once and thereby ensuring that all reachable code is executed. That is, every decision is taken each way, true and false.

Which statement about the relationship between statement coverage and decision coverage is true?

Which of the following statements about relationship between statement coverage and decision coverage is correct? 100% decision coverage always mean 100% statement coverage.

Is decision coverage automatically covers statement coverage?

100% decision coverage implies both 100% branch coverage and 100% statement coverage. Test coverage criteria requires enough test cases such that each condition in a decision takes on all possible outcomes at least once, and each point of entry to a program or subroutine is invoked at least once.


1 Answers

The answer by Paul isn't quite right, at least I think so (according to ISTQB's definitions). There's quite significant difference between statement, decision/branch and condition coverage. I'll use the sample from the other answer but modified a bit, so I can show all three test coverage examples. Tests written here gives 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 have to test each statement at least once, so we need just two tests:
    • a=true b=false - that gives us path if(a||b) true -> test1 = true
    • a=false, b=false and c=true - that gives us path: if(a||b) false -> else -> if(c) -> test2=true.

This way we executed each and every statement.

  1. branch/decision coverage needs one more test:

    • a=false, b=false, c=false - that leads us to that second if but we are executing false branch from that statement, that wasn't executed in statement coverage

    That way we have all the branches tested, meaning we went through all the paths.

  2. condition coverage needs another test:

    • a=false, b=true - that leads through the same path as first test but executes the other decision in OR statement (a||b) to go through it.

That way we have all conditions tested, meaning that we went through all paths (branches) and triggered it with each condition we could - first 'if' statement was true in first test because of 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 will 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.

like image 130
Faflok Avatar answered Sep 26 '22 23:09

Faflok