Coverage info displayed on Test code

Must-share information (formatted with Markdown):

Env:

  • SonarQube Community Edition Version 8.4 (build 35506)
  • Jacoco 0.8.5 for unit test report

Expected: Coverage info displayed only on main code, not on Test code.
Actual: Lines to Cover on New Code displayed on Test code.

What’s possible reason for this?

In the detail view, it shows that those 7 “Uncovered Lines on New Code” actually are some kind of business logic code in main folder. See below pic:

Hi,

The icons tell the tale here:

Selection_999(249)

There’s a “code file” icon on your test file, rather than a “test file” icon. This indicates that the configuration passed into analysis identified this file and/or parent directory as source rather than test. Sort this out and you’ll have it solved.

The sonar.scanner.dumpToFile=[path to file] parameter may be of help here. Per the docs, it

Outputs to the specified file the full list of properties passed to the scanner API as a means to debug analysis.

 
HTH,
Ann

Thanks for the clarification.

I always pass project base directory as parameter value of sonar.source, written as sonar.source=. (Maybe this is a bad practice, but it works well until I encountered this problem mentioned here.)

In my case, it seems that:

  • if the class file under src/test/java folder ends with Test, it will be treated as “Test Code”,
  • otherwise, recognized as “Source Code”. such as the one mentioned in red rectangle picture.

later on, I try to verify this guess.


So, maybe it’s better to leave it alone. Let it read from build system for Maven?

Hi,

If you’re building with Maven but manually specifying your basic analysis params, then yes. It’s time to analyze via Maven & let those values be read from the build system:

mvn sonar:sonar

 
Ann

Actually, We use sonar-scanner.

INFO: Scanner configuration file: /usr/local/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 1.8.0_231 Oracle Corporation (64-bit)
INFO: Linux 4.19.2-1.el7.elrepo.x86_64 amd64

and the command like belowing, in which the value of sonar.source is set via -Dsonar.source=.

sonar-scanner -Dsonar.projectKey=sale_java:car_service:develop -Dsonar.projectName=sale_java:car_service:develop -Dsonar.sourceEncoding=UTF-8 -Dsonar.host.url=http://10.16.210.102:9000 -Dsonar.login=0a0543988eca85fdbc5bf4ab9455eecd29a306dc -Dsonar.projectVersion=BASELINE -Dsonar.projectDate=2020-09-02 -Dsonar.java.binaries=. -Dsonar.language=java -Dsonar.java.source=1.8 -Dsonar.sources=.

Thanks anyway :slight_smile:

So my guesses are correct in this scenario?
Classes under src/test/java without “Test” suffix will be recognized as “Source Code”?

I update sonar.source to /src/main/java, now Classes under src/test/java won’t be recognized as “Source Code” anymore.