Sonar does not shows up Code Coverage after build successful with Jenkins Sonar plugin - code-coverage

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.

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>

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.

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
[...]

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!

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?

Related

Where does sonar store its code coverage results in Jenkins workspace?

We have a Jenkins master hosted by Cloudbees and we are using Sonar-as-a-Service.
We have a regular maven project, and we are using the Sonar "Post Build Action" to successfully collect code coverage. I see the code coverage % in Sonar. Everything works fine.
My question is this: If I examine the Jenkins workspace of this project, I cannot find the code coverage results. I thought it would be saved in an xml file somewhere in the target directory, but i do not see it. Does sonar save the results in the Jenkins workspace somewhere?
Thanks!
Sonar uses Jacoco by default for test coverage, this one uses a binary file afaik

Jenkins Sonar jacoco

My JUnit testcoverage is not propagated to sonar when built on jenkins. The "Unit test coverage" field on sonar stays empty, the "Unit test success" field shows the correct values though.
I'm using jacoco for the testcoverage. On jenkins, the report of jacoco works well and shows the correct coverage in the generated html. I just can't get it to sonar.
With the jacoco plugin from jenkins, I invoke a standalone Sonar Analysis with the following parameters.
# required metadata
sonar.projectKey=ePortal:Ejb
sonar.projectName=ePortal
sonar.projectVersion=1.0
# path to source directories (required)
sonar.sources=ePortalEjb/ejbModule
# path to test source directories (optional)
sonar.tests=ePortalEjb/src/test
sonar.core.codeCoveragePlugin=jacoco
sonar.surefire.reportsPath=ePortalEjb/junit
sonar.forceAnalysis=true
sonar.jacoco.reportPath=ePortalEjb/build_test/jacoco/jacoco.exec
sonar.dynamicAnalysis=reuseReports
sonar.jacoco.antTargets=test
Paths and ant-tasks are correct. Maybe I am missing something obvious?
Thanks in advance for any help.
Greetings Redmo
As you're not triggering the Sonar analysis with Ant but with the Sonar Runner, could you please remove the 'sonar.jacoco.antTargets' property from your project configuration file?
Do you confirm that the JaCoCo report has been generated and dumped at ePortalEjb/build_test/jacoco/jacoco.exec prior to the Sonar analysis?
Which version of Sonar are you running?
Could you provide the log of the analysis in debug mode (-X)?
Could you run the same analysis outside of Jenkins and confirm that you encounter the same issue?
Thank you
David RACODON | SonarSource

JaCoCo Selenium test code coverage and import to Sonar using Ant

I am running Sonar task through Ant, triggered by Jenkins in RHEL environment. I am successfully using Cobertura for Junit code coverage and Surefire for reporting. Sonar imports the Surefire reports fine.
However, now I am running Selenium tests using Ant in Jenkins. I would like to report code coverage and test results to Sonar. Apparently I need the JaCoCo plugin which analyses code coverage and reports tests. I presume like for unit tests, Jenkins does the job and Sonar only imports the reports into its own repository.
I am puzzled on how to actually do this. The web page http://www.sonarsource.org/measure-coverage-by-integration-tests-with-sonar-updated/ references to the JaCoCo page http://www.eclemma.org/jacoco/trunk/doc/ant.html. I am not sure what the steps are to be done. Do I need the coverage target? Should I only start the agent? Where in Ant do I start the agent? Do I need to dump stuff?
I really appreciate all the help I can get, thanks :)
Sonar 3.3 has a new feature for combine code coverage metrics generated by both unit tests and integration tests. This is done by using two properties to detail the two different report files generated by the jacoco too:
#Tells Sonar where the unit tests code coverage report is
sonar.jacoco.reportPath=reports/jacoco/jacoco-ut.exec
#Tells Sonar where the integration tests code coverage report is
sonar.jacoco.itReportPath=reports/jacoco/jacoco-it.exec
The Sonar examples project has an integration test example for ANT:
https://github.com/SonarSource/sonar-examples/tree/master/projects/code-coverage/it/ant/it-jacoco-ant
Unfortunately it doesn't give an example of running the actual tests, instead it just shows how to configure an ANT build to load data.
Finally the Sonar documentation has more details with links to the example projects.

Sonar displaying 0% Emma coverage report

I agree that this question is asked many a times by many people and many solutions are already been provided. However, after referring to umpteen links, not being able to display the Emma report in Sonar is driving me crazy. Below is a description of what I am trying to do.
My java project is a multi module maven project. We are using EMMA as the code coverage tool and Jenkins to build the project, Sonar to analyze the code and configured Sonar for our build job using the Jenkins sonar plugin. Up to this point, the story is great. The problem comes when I try to reuse the reports generated by Emma during Sonar analysis. There is no apparent error in the Sonar analysis logs during the build, however, in Sonar project dashboard the coverage report is 0%. Below is configuration data that I provide in the Sonar configuration section of my Jenkins build job.
-Dsonar.core.codeCoveragePlugin=emma
-Dsonar.emma.reportPath=/target
-Dsonar.dynamicAnalysis=reuseReports
-Dsonar.surefire.reportsPath=/target/surefire-reports
Sonar Version : 3.0
Emma plugin versions:
emma-maven-plugin : 1.0-alpha-2
Sonar Emma Plugin :1.0.1
emma : 2.0.5312
The Maven build generates the coverage.em files in the individual modules' target folder. No coverage.ec file is generated. Is it mandatory to have both coverage.ec and coverage.em files for Sonar?
The coverage.es,html,txt,xml files are generated in the project build directory/emma folder
The Sonar analysis logs for a module looks like below.
[INFO] [15:06:02.829] Sensor org.sonar.plugins.emma.EmmaSensor#bfc33...
[WARN] [15:06:02.844] Resource will be ignored in next Sonar versions, index is locked: com.mycompany.mypackage.tests.util.Activator
[INFO] [15:06:02.876] Sensor org.sonar.plugins.emma.EmmaSensor#bfc33 done: 47 ms
The above logs make me think that Sonar is indeed doing something with the generated Emma reports, but not displaying anything on the Sonar project dashboard.
Please let me know where I may be going wrong or what additional thing needs to be done to get this working.
I think this thread might me useful. http://sonar.15.n6.nabble.com/Receiving-quot-index-is-locked-quot-warning-when-Emma-sensor-runs-td4676490.html
It describes the same problem and gives you some workarounds

How to get JaCoCo coverage with Sonar in Jenkins?

I followed the instructions here
I am NOT using Maven.
My Jenkins job output says:
12:32:33.951 INFO Sensor JaCoCoSensor...
12:32:33.961 INFO Project coverage is set to 0% as no JaCoCo execution data has been dumped: /var/lib/jenkins/workspace/SeqGen/SeqGen/jacoco.exec
12:32:35.152 INFO Sensor JaCoCoSensor done: 1201 ms
Here are my properties:
project.home=SeqGen
sonar.projectKey=com.skyboximaging:seqgen
sonar.projectName="SeqGen"
sonar.projectVersion=1.0
sonar.dynamicAnalysis=true
sources=src/java
tests=test/java
binaries=classes
sonar.jacoco.reportPath=jacoco.exec
sonar.jacoco.antTargets=test-with-coverage
On the Sonar server, I set General Settings > Code Coverage > Code coverage plugin to jacoco
I am very confused by the documentation at http://docs.codehaus.org/display/SONAR/Code+coverage+plugins I am particularly puzzled by this sentence:
"During Sonar analysis, the Sonar Jacoco plugin will take care to attach the Jacoco agent to the JVM and to launch the unit tests."
As far as I can tell, my Ant target is not getting invoked. How does the Sonar Jacoco plugin know where to find my build.xml?
What am I doing wrong?
They just released what you need : https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin
Providing an answer for the next poor soul who spends hours trying to find out why this doesn't work. Apparently this feature has been removed:
Remove "sonar.jacoco.antTargets" property and related classes in the plugin
One possibility is that the name of your ant task for junit does not match the name specified in the properties.
You have specified
sonar.jacoco.antTargets=test-with-coverage
As per the documentation, there should be an ant target with exactly the same name. Also in this target, junit should be set to fork as well.

Resources