6

I'm using Gradle sonarqube plugin and I need to exclude all test sources from the sonar analysis (main goal is to filter out unit test classes from the issues/code smells report)

To do so, I have used the dedicated sonar.test.exclusions property as follow, to exclude the whole src/test directory from analysis

sonarqube {
    properties {
        property("sonar.exclusions" , "")
        property("sonar.test.exclusions" , "src/test/**/*.java")
        // other sonar properties, omitted
    }
}

This works as expected (test sources are filtered out) BUT : when this property is set, sonar does not compute/report number of unit tests correctly.

See simple example for a very basic project: 2 main source files, 1 test source file containing 2 Junit tests (and also containing some issues I don"t want to see in report)

  1. Without exclusions:

Sonar properly reports my 2 unit tests, but it also includes code smells from the unit test class

enter image description here

  1. With exclusions:

Now, code smells from the unit test are properly filtered, but I lost the Unit test count information enter image description here

Notes:

  • using Gradle 6.7, sonarqube plugin version 3.0, and sonar server Community EditionVersion 8.4.2
  • also tried with the property sonar.exclusions : same problem
  • all other sonar properties are correctly set and have same values in both scenarios : specially sonar.tests, sonar.java.test.binaries, sonar.junit.reportPaths, sonar.jacoco.reportPath

Any idea how to configure the sonarqube plugin, to exclude properly test sources, while keeping Unit tests information available?

M.Ricciuti
  • 11,070
  • 2
  • 34
  • 54

2 Answers2

3

I agree with Chriki's answer but only the first part : using sonar.test.exclusions is not the good way.

I disagree with the last part : using sonar.issue.ignore.multicriteria is totally possible with Gradle

https://www.baeldung.com/sonar-exclude-violations#using-sonar-projectproperties

Try something like this (not tested from my end though) :

sonarqube {
    properties {
        property "sonar.issue.ignore.multicriteria", "e1"
        property "sonar.issue.ignore.multicriteria.e1.resourceKey", "src/test/java/**/*"
        property "sonar.issue.ignore.multicriteria.e1.ruleKey", "*"
    }
}
ToYonos
  • 16,469
  • 2
  • 54
  • 70
1

What you’re after doesn’t seem to be possible from a Gradle configuration. Let me elaborate on how I came to that conclusion.

In a (admittedly very) old thread on the Sonarqube mailing list from 2013, somebody asked the same question (albeit for Maven). A Sonarqube consultant has answered it as follows:

When you exclude a file (or a test file) with sonar.exclusions (or sonar.test.exclusions), the file is just ignored by SonarQube. The source code is not imported, no metrics are computed on this file (such as number of tests).

To ignore some specific issues on specific files, see http://docs.codehaus.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreIssues. To ignore some files to be taking into account for code coverage computation, see http://docs.codehaus.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreCodeCoverage and so on.

The current documentation link corresponding to the first (now broken) one from the quote is this: https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/#header-3 (or for version 8.4) It’s about ignoring “issues on certain components and against certain coding rules” – which is what you’re after, if I’m not mistaken. However, these docs state right at the beginning:

Note that the properties below can only be set through the web interface because they are multi-valued.

Here “web interface” is meant as opposed to a (Gradle) build configuration for example. In fact, the previously mentioned Sonarqube consultant explicitly states this for Maven, too:

The property is sonar.issue.ignore.multicriteria. I think I read somewhere that you have to do these through the GUI because they are multi-valued? Is that the case? If not, how would I write it as an entry in the pom.xml? We like to keep all config in the one place (pom.xml).

It's unfortunately not possible.

The mailing list discussion is a bit longer and gives more insight, so it may be worth a read in full. It also sheds some more light on the sonar.test.exclusions property for which I could otherwise not find any good documentation.

Chriki
  • 15,638
  • 3
  • 51
  • 66
  • 1
    hi @Chriki, thank you for your detailed answer, the links you provided are useful: the use of `sonar.issue.ignore.multicriteria` is the good way to go. As described in the other answer, it's even possible to configure these properties from gradle, which is what I needed. BR – M.Ricciuti Jan 04 '21 at 17:12