0

Example 1

if ( a1 || a2 || ... || an ) {
    ...
}

Sonar shows that 0/2*n branches are covered by tests. (E.g.: 0/6 for n=3)

Let's take a function:

f:{a1,...,an}->{true,false}

The number of functions (=all possible combinations) is equal to |{true,false}|^|{a1,...,an}| = 2^n which is obviously different than 2*n ( 8 != 6 for n=3). It could be demonstrated using a sum of combinations, too.

If we want to write that if in the following way:

if ( a1 ) {
   ...
} else if ( a2 ) {
          ...
       } else if ( a3 ) {
          ...
       }
       ...

we can cover all branches using the following n sequences - example for n=3:

  • T**
  • FT*
  • FFT

where T=True, F=False, *=don't care

Q: So, why is there 2*n ?

Example 2

if ( a != null && a.length > 0 ) {
   ...
}

Sonar shows that 0/4 branches are covered by tests.

Q: How can I achieve 4/4 ? I see only 3 possible cases (inputs):

  1. a != null, a.length > 0
  2. a != null, a.length <= 0
  3. a == null (the second condition cannot be adjusted)

I know that SonarQube uses another tool like Cobertura or JaCoCo for coverage and this tool is responsible for the above issues, but I didn't specify it because I don't know it. According to these features it seems that probably the coverage tool is Cobertura or JaCoCo because they have line and branch metrics.

I'd like to know the principle (how they decided to use those metrics).

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

1 Answers1

1

In SonarQube, covered branches (we call them "covered conditions" in the Metrics Definition page) are indeed given by third party tools. SonarQube trusts those tools, so it just parses the coverage reports and then aggregates the "raw" data to compute the branch coverage.

By default, SonarQube comes with JaCoCo (embedded in the Java Plugin) - but there are other plugins like the Cobertura plugin.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199