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

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

Related

Testing Jenkins "Code Coverage API Plugin"

I am new to Jenkins, I have installed "Code Coverage API Plugin" in Jenkins, now want to test it.
I am trying to do steps from this page https://plugins.jenkins.io/code-coverage-api/.
But I did not understand where to put XML files from points 1 and 2.
Can anybody help me, please?
The tags present in the images are the plugins that will be added in the pom.xml of the application. By running mvn clean package cobertura will generate a report based on the formats provided, xml or html.
If the code is checked out to the job and after running "mvn clean package" cobertura report will be generated under project workspace target/site/cobertura
The xml path is provided to code coverage api to provide the graphical representation.
Jacaco is preferred for java versions >=8 whereas cobertura doesn't cover for java8 based functionalities.

How to get modules' coverage via sonar runner in jenkins

What we have:
jenkins 1.6 + SonarQube 4.5.6 + sonnar-runner 2.4 + maven 3.0.5
Issue Description:
We use 'Invoke Standalone SonarQube Analysis' in jobs' configuration, and it works fine except that it cannot get test coverage while the analysis just try to parsing the <base_dir>/target/site/cobertura/coverage.xml. But we have coverage.xml in the modules directory instead, just like <base_dir>/module1/target/site/cobertura/coverage.xml
I have tried to set sonar modules, but nothing happened, we still cannot get the coverage data.
Previously, we used sonar-maven-plugin for the analysis, but after we upgrade to SonarQube 4.5.6, we met some issues, like api incompatibility, measure twice, and so we changed to use sonar-runner. sonar-maven-plugin works fine to parsing the modules' coverage.xml.
How can we retrieve the coverage data in the modules' directory.
Or how can we merge the modules coverage.xml into one coverage.xml in the root directory for sonar-runner to parse.
Any comment? Thanks very much!

Generate two coverage reports in a single jenkins build

I have a Jenkins build which build all my java/angularJS project. It launch testNG tests for the java part and karma tests for the javascript part. So I can generate one testNG report (for java) and one junit report (for karma test) in my Jenkins build. This is working very well.
Until now, I used cobertura to report the coverage of my java tests. But now I would like to add also a coverage report for my karma tests (generated by Istanbul with cobertura type). The problem is that, in Jenkins, I'm allowed to generate only one coverage report in a build (I can't add more that one 'publish cobertura coverage report' post build action). So how can I have these two coverage reports in a single Jenkins build?
There's a nice plugin called HTML Publisher Plugin. You can generate HTML coverage reports and publish as much reports as you want under different titles in one Jenkins project.
For example I generate html reports using karma+istanbul and then publish them to Jenkins.
On JUnit xml report files. You should import JUnit once enumerating all files probably from different directories but you can differentiate them with proper package names inside files.
If I'm right, you can't use, as a post build action, the same plug-in twice( note that I'm not really sure). I faced this problem when I worked as Jenkins plug-in developer for a company and the solution was to use a plug-in that make the same thing.
For example: for JUnit reports there is an official JUnit plugin and also XUnit. For my problem it was simple.
So, maybe you can find a plug-in that do the same thing as Cobertura or you can change the output format of the java coverage or karma coverage. For example, for java you can use EclEmma or Jacoco...

How does the Jenkins Cobertura plugin operate to enable source code viewing?

I'm trying to get a build in Jenkins to show a coverage report of a Java unit test run. I can get the report at the high level showing that coverage has occurred at the class and method level, but part of the report where I can view the source code isn't working.
The inputs to the plugin are:
a coverage report in XML
source code
To use the plug in to create a coverage report:
Install Cobertura into Jenkins.
Create a Jenkins job with a post build to generate Cobertura report. In this step you tell it where in the Jenkins workspace to load the coverage report (often named coverage.xml).
Before you run the Jenkins job
The coverage report is created in some way (often by a build script of some kind). The report is often named coverage.xml by most people's build scripts and is placed in the Jenkin's job's workspace.
When running the Jenkins job
When the Jenkins job runs, the Cobertura plugin will read the coverage.xml and use the element to locate the source code. The plugin copies the source code into a cache located at: <Jenkins home>/jobs/<Jenkins job name>/<Cobertura>.
After running the Jenkins job
Read the coverage report by navigating to the Jenkins job that built it. If copying of the source files was successful during running the Jenkins job, you can drill down to the source code and see what lines were covered by the test run. If you can't see the source code level code coverage:
Check the cobertura cache to see if the code is there.
Check the permissions on the cache to see if the Jenkins server process owns that directory and has read, write, execute permission on that directory.
Check the cobertura.xml file and select a element and examine it's "filename" attribute and see if it's path will work as a relative path from the root of the Cobertura cache.
If the cache has old information, delete everything in the cache directory, and re-run the Jenkins job.

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

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?

Resources