2

I have set up SonarCloud analysis for a Java project which triggers analysis when a PR is created on the git repository.

The command for triggering the check in my .travis.yml file is:

mvn clean compile test dependency:copy-dependencies
sonar-scanner 
-Dsonar.projectKey=project-key 
-Dsonar.java.binaries=project/target/classes 
-Dsonar.java.test.binaries=project/target/test-classes 
-Dsonar.java.libraries=project/target/dependency 
-Dsonar.java.test.libraries=project/target/dependency 
-Dsonar.test.exclusions=project/src/test/**/*Test.java

What is expected
If someone checks-in code in a Pull Request that has a test class then it should not be considered for coverage. Only the target class should be.

What is happening
If I check-in 2 classes SonarSample.java and SonarSampleTest.java then SonarCloud shows coverage for both the classes.

enter image description here How can I exclude the test class from being considered for coverage data?

Abhijeet Vaikar
  • 1,578
  • 4
  • 27
  • 50

2 Answers2

0

Note that coverage is displayed for files classified as sources, and not displayed for files classified as tests.

This looks like a mistake:

-Dsonar.test.exclusions=project/src/test/**/*Test.java

If you want *Test.java files to be treated as tests, and thereby exclude them from sources and therefore coverage computations, then change that line to be an inclusion instead of an exclusion:

-Dsonar.test.inclusions=project/src/test/**/*Test.java

As an aside: why are you using sonar-scanner instead of mvn sonar:sonar?

janos
  • 120,954
  • 29
  • 226
  • 236
0

In my root pom.xml, adding following two lines resolved the issue,

        <sonar.sources>**/main/java/**/*</sonar.sources>
        <sonar.inclusions>**/main/java/**/*</sonar.inclusions>

This doesn't count *Test.java files from src/test folder in coverage report.

jdk
  • 451
  • 1
  • 6
  • 18