8

SonarQube describes the "Condition" coverage like this:

On each line of code containing some boolean expressions, the condition coverage simply answers the following question: 'Has each boolean expression been evaluated both to true and false?'. This is the density of possible branches in flow control structures that have been followed during unit tests execution.

http://docs.codehaus.org/display/SONAR/Metric+definitions

Well but I suspect they mean "branch coverage":

if (A || B || C)

Testing A=true and B=true yields 100%, without the need of checking the last expression (C). Also just two branches are tested - true and false for the whole expression, not individual expressions. Is that right?

As far as I know, condition coverage should check all conditions in an expression.

Delgan
  • 18,571
  • 11
  • 90
  • 141
Pietross
  • 313
  • 1
  • 3
  • 9

2 Answers2

4

You can read the following thread of discussion: http://sonarqube.15.x6.nabble.com/I-can-t-understand-the-meaning-of-quot-condition-coverage-quot-in-SonarQube-tt5029339.html

  • Well they say the same but I do not understand their arguments. If a condition has n expression (if A || B || C || D) then there are still only two branches (true and false outcome) and you would need just two test cases. – Pietross Nov 12 '14 at 09:06
  • That's why I think it's more branch coverage than condition coverage. In this thread, I asked SonarSource why they changed the label but they haven't replied yet. – David RACODON - QA Consultant Nov 12 '14 at 09:09
  • Thanks, maybe I misread that. But I remember you were shown 0/4 for (X && Y), which would not make sense. For branch coverage, it should be 0/2 with no test. Condition coverage test cases can be calculated: 2^N where N means components of a compbound condition. – Pietross Nov 12 '14 at 10:08
  • if (A | B) => 2 branches, true or false. But || or && operators increase the number of branches. Indeed the second operand may or may not be evaluated. – David RACODON - QA Consultant Nov 12 '14 at 10:44
  • I do not think so. The result of the compbound condition will always be true or false, regardless how many operators there are because there are only two branches of the code - IF and ELSE, no other branches. If you look at the control flow graph, there are two edges. – Pietross Nov 12 '14 at 14:07
  • Nope. Let's take the following example: function f() { if (A || B) {...} else {...}}. The cyclomatic complexity (number of branches) is 3, not 2. If condition A is true, condition B won't be executed. If condition A is false, condition B will be executed. Then the overall condition is either true or false. – David RACODON - QA Consultant Nov 12 '14 at 14:12
  • Not really, cyclomatic complexity determines PATHS, not branches. Branches are always (control flow graph ca show that well) just 2 from an IF statement. Paths are combinations between nodes. – Pietross Nov 12 '14 at 17:18
3

For me on SonarQube 5.6 condition coverage is really the same as path coverage as defined e.g. here: http://www.onjava.com/pub/a/onjava/2007/03/02/statement-branch-and-path-coverage-testing-in-java.html?page=2

For example, my test report points out that on an if-condition that checks two boolean flags, it insists that all 4 possible combinations have to be checked before 100% coverage is attained.

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60