sonar-scala / sonar-scala

A free and open-source SonarQube plugin for static code analysis of Scala projects.

Home Page:https://sonar-scala.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't get junit report to work with test coverage

Symbianx opened this issue · comments

Hello,
Thanks for all the amazing work put into this plugin, it's extremely useful!

I managed to get the statement coverage and code smell reports working in sonarqube but I'm failing to get the number of unit tests run working.

Our project uses sbt and generates the test reports as expected but I can't seem to get all the sonar sensors working at the same time.

Am I doing anything wrong here? Is this not a supported use case?

What I tried so far follows:

First try

Setting the following properties result in the code coverage and code smells being reported in sonarqube but not the test execution report (how many failed, how many succeded, etc):

" -D sonar.scala.scoverage.reportPath='out/scoverage-report/scoverage.xml'" +
" -D sonar.scala.scapegoat.reportPath='out/scapegoat-report/scapegoat.xml'" +
" -D sonar.junit.reportPaths='out/test-reports'" +
" -D sonar.verbose=true"

The debug output prints

DEBUG: 'Scala JUnit Sensor' skipped because there is no related file in current project
[...]
Sensors : CSS Rules -> Scala Sensor -> Scalastyle Sensor -> Scapegoat Sensor -> Scoverage Sensor -> Go Cover sensor for Go coverage -> JaCoCo XML Report Importer -> C# Properties -> JavaXmlSensor -> HTML -> XML Sensor -> VB.NET Properties

Second try

To the existing properties I tried adding the sonar.tests property pointing to the test directory which results in the unit test execution being report, but now the all scala sensors except the JUnit Report are not executed at all:

" -D sonar.tests='src/test/scala/taxonomy'" +
" -D sonar.scala.scoverage.reportPath='out/scoverage-report/scoverage.xml'" +
" -D sonar.scala.scapegoat.reportPath='out/scapegoat-report/scapegoat.xml'" +
" -D sonar.junit.reportPaths='out/test-reports'" +
" -D sonar.verbose=true"

The debug output includes

DEBUG: 'Scalastyle Sensor' skipped because there is no related file in current project
DEBUG: 'Scapegoat Sensor' skipped because there is no related file in current project
DEBUG: 'Scoverage Sensor' skipped because there is no related file in current project
[...]
DEBUG: Sensors : CSS Rules -> Scala Sensor -> Scala JUnit Sensor -> Go Cover sensor for Go coverage -> JaCoCo XML Report Importer -> C# Properties -> JavaXmlSensor -> VB.NET Properties

Hi @Symbianx and thanks for your kind words!

If you're using default/conventional directory structure in your project (regardless of whether it's a single or multi-module scala project) then set sonar.tests to src/test/scala (it looks like your setting is pointing to src/test/scala/taxonomy) and you should be good - see the following examples for a reference.

Also have a look at the sbt-sonar plugin which can take care of automatically setting all of those properties for you, see the docs here.

Hi @mwz,
Thanks for the quick response!
After setting the sonar.tests to src/test/scala, it still didn't work. Our tests are all in the src/test/scala/taxonomy directory. After moving the tests to src/test/scala the junit report sensor seems to pick them up.

I also had to set sonar.sources to src/main/scala for the code smells and coverage scanning to work and it picked up the source files inside the src/main/scala/taxonomy folder without a problem.

Is having the test files inside a sub directories not a supported use case or is it a bug? If it is a bug, I wouldn't mind helping with a PR if you can point me in the right direction.

Is having the test files inside a sub directories not a supported use case or is it a bug? If it is a bug, I wouldn't mind helping with a PR if you can point me in the right direction.

From what I remember those test reports contain package names not directory structure, so your folder structure needs to reflect your package naming for the sensor to recognise those test files - so if your package name is taxonomy and all your tests are inside src/test/scala/taxonomy setting sonar.tests to src/test/scala should work fine, unless perhaps you're missing package declarations in your test files, but the files are still located within the taxonomy directory, which could potentially break this. I can't think of anything else right now, but if that isn't the issue then it would definitely help if you could put together a minimal example project to replicate this issue and I'd be happy to take a look and maybe we can get to the bottom of this, it might as well be a bug in the JUnitSensor or the JUnitReportParser.

Wow, you're absolutely right. My test file was missing the package declaration... 🤦
Thanks so much for all the help @mwz !

no worries, glad you got that sorted out!