15
votes

I am trying to get code coverage with Sonar and Jenkins. I see Jenkins' Sonar plugin successfully executes JUnit test cases and completes build successfully. But Sonar does not show Code Coverage results (always shows 0.0% as the code coverage) on the project. But Sonar does show "Unit test success".

I am using Maven with Jenkins and Sonar.

I get the below message in Jenkins logs while executing the Sonar plugin:

Project coverage is set to 0% as no JaCoCo execution data has been dumped: .../sonar/target/jacoco.exec

Can any one help me how to get correct code coverage on any Sonar project.

5
Did you read docs.codehaus.org/display/SONAR/… and give a try to related project samples? If so, could you please provide more detailed information: log file, configuration of SonarQube in your pom file, how do you run SonarQube: mvn clean install / mvn sonar:sonar? Versions of SonarQube, etc.?David RACODON - QA Consultant
I read docs.codehaus.org/display/SONAR/… and used cobertura as my code coverage plugin then I see code coverage displays for small projects. When I check for big project in sonar I just see code coverage as - that means its empty.in logs I could find Cobertura report not found at .../coverage.xml path. What would be the reason for it. cobertura plugin instrumentation went fine before showing above message.Venkat
coverage.xml was not generated due to OutOfMemeryError:heapspace. Since my project is such a big project when I set heap memory to 2GB and cobertura plugin memory to 1.5GB sonar gets code coverage displayed.Venkat

5 Answers

11
votes

Just because Sonar invoked Surefire correctly (and you received the "Unit test success" message) doesn't mean that JaCoCo instrumented your code.

Try executing JaCoCo directly. You might find out why JaCoCo is failing directly:

mvn jacoco:prepare-agent test jacoco:report

JaCoCo will place jacoco.exec as well as its XML/HTML reports within target/jacoco. Or it will fail, and hopefully you'll have a better idea as to why.

One very common problem is that the JaCoCo javaagent will not run if you've changed the Surefire argLine at all, because jacoco:prepare-agent just sets the argLine property which in this scenario, is conveniently ignored. You can set prepare-agent's propertyName property to something else (like jacocoArgLine) and include that in your argLine config:

<argLine>-Xmx1024m ${jacocoArgLine}</argLine>

3
votes

I read https://docs.sonarqube.org/display/PLUG/Code+Coverage+by+Unit+Tests+for+Java+Project and used cobertura as my code coverage plugin then I see code coverage displays for small projects. When I check for a big project in sonar I just see code coverage as - that means its empty. In logs I could find that Cobertura report was not found at /.../coverage.xml path.

coverage.xml was not generated due to OutOfMemeryError:heapspace. Since my project is such a big project when I set heap memory to 2GB and cobertura plugin memory to 1.5GB sonar gets code coverage displayed.

2
votes

According to this blog post, you probably forget to set the sonar.binaries property in your project properties

Don’t forget sonar.binaries, otherwise, you might get something like « Project coverage is set to 0% since there is no directories with classes. » in your logs.

[...]
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/jacoco.exec
sonar.binaries=target/classes
[...]
2
votes

Your Sonar plug-in cannot find the report file generated by Jacoco.

In your pom(parent or child) look for the <destFile> tag under jacoco/prepare-agent execution. Change the name and the location to target/jacoco.exec and you are good to go!

0
votes

Instead of using the Jenkins sonar plugin for running sonar, try to do it with maven, by executing the sonar goal. Usually this is easier then setting up an additional build step and tweaking all the paths till sonar finds all the relevant files.

In your case I guess it can read the junit/testng report, but can't find the jacoco results (jacoco.exec). Is the path sonar outputs(../sonar/target/jacoco.exec) correct?