Integrating Sonar with native Teamcity Code coverage (Java) - java

sonar is not picking up the code coverage reports generated by IntellijIdea/Jacoco coverage reports.
Heres one sample of me trying to get sonar to pickup the jacoco report generated
Build Step:
Step 1
Ant
Build file path: junit-build.xml
Targets: default
JaCoCo coverage enabled
Execute: If all previous steps finished successfully
Step 2
SonarQube Runner
Execute: If all previous steps finished successfully
Artifact:
.teamcity
coverage_jacoco
jacoco.exec
UT JaCoCo Report: .teamcity/coverage_jacoco/jacoco.exec
JaCoCoSensor: JaCoCo report not found : /export/.../BuildAgent2/work/.../.teamcity/coverage_jacoco/jacoco.exec
Sonar:
This component does not have coverage details.

The issue is that the TeamCity jacoco runner places the output files in a temporary file location that can't be found by the sonar plugin or by sonar when run as a part of the maven/ant build. ".teamcity" is in the published artifacts store not the build area and before publishing the jacoco output appears to be in a randomly named directory.
The alternative is to turn off the teamcity jacoco runner and trigger the coverage and sonar scan via the maven or ant build itself. Sonar can be configured to find the results by putting the correct location in the administration->java->jacoco settings (relative to the build or module root) This will get your coverage into sonar.
To add the coverage report back into teamcity you need to add an artifact in your build general settings which collects the report html files into a coverage.zip file. Then you'll get a Code Coverage tab in your build results. (If you get two then you're still running the teamcity coverage runner as well.)
in short for maven builds (ant will be logically the same):
turn off the coverage option in the teamcity build step
add the org.jacoco:jacoco-maven-plugin:prepare-agent goal to create a report consumable by sonar
In sonarqube set the jacoco UT report to "target/jacoco.exec"
add the org.jacoco:jacoco-maven-plugin:report goal to create an html report for use by teamcity
add this artifact "target/site/jacoco/**/. => coverage.zip" to make the html report appear as a tab in teamcity
For multi-module builds where you have more than one report I can't find a way to get these into teamcity - though they do look fine in sonar. There is a jacoco merge goal that may help but I haven't been successful with it - and I suspect it'll mess up the sonar view.

Related

SonarQube 8.2 Analysis shows 0 code coverage

SonarQube: 8.2.0.32929
sonar-scanner: 3.0.3.778
jacoco: 0.8.4
jdk: 1.8
mvn: 3.6.3
What are you trying to achieve
I am trying to achieve code coverage by using sonar-scanner but I am getting code coverage 0 in sonarqube dashboard.
What have you tried so far to achieve this
I configured the multi-module java project using https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-multimodule
and created sonar-project.properties file in base directory with below configuration
sonar.projectKey=org.sonarqube.sonarscanner-maven-aggregate
sonar.projectName=Sonar Scanner Maven Aggregate
sonar.projectVersion=1.0
sonar.language=java
sonar.java.binaries=.
If I use mvn sonar:sonar it works. but with sonar-scanner it is not working.
It works fine with sonarqube 7.8.
Any insight would be appreciated.
I fixed this issue.
while running sonar-scanner command, I added the xmlReportPaths as a define property like sonar-scanner -Dsonar.coverage.jacoco.xmlReportPaths=tests/target/site/jacoco-aggregate/jacoco.xml,../tests/target/site/jacoco-aggregate/jacoco.xml
If your projects are set up as multi-module, you will have to feed the coverage report from each module to the last module that will run as part of the build (probably integration tests modules)
https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151
https://community.sonarsource.com/t/in-sonarqube-8-2-code-coverage-is-always-showing-0/21666
https://docs.sonarqube.org/latest/analysis/coverage/
https://stackoverflow.com/a/15535970
Following the steps mentioned in this example project by Sonar team helped me
https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-multimodule
As you already followed these steps, one thing I was missing was the aggregated report was not generated in the last module and I was doing it in one module before that which was our integration test module. But there was one more deployment module in our case

Gradle Plugin to generate cucumber report not compiling

I am writing Cucumber Automated Test Cases in java, using REST Assured.
All the tests run as JUnit tests, & as Gradle tests, & they all pass successfully.
The next step I need to take is to generate a Cucumber Report based on these test results.
I've been told that I need to find a gradle plugin for cucumber.
So far, I've tried putting the following code (from here: https://plugins.gradle.org/plugin/com.github.spacialcircumstances.gradle-cucumber-reporting) into my build.gradle file:
plugins {
id "com.github.spacialcircumstances.gradle-cucumber-reporting" version "0.0.12"
}
But, when I try to refresh the gradle, I get this error:
Sychrnoize Gradle projects with workspace failed due to an error in the referenced Gradle build.
Is this the correct plugin to generate this cucumber report, and if so, why is it not being accepted by the gradle?

Using Sonar in Ant only for code coverage with JaCoCo (no static analysis)

I created a job in Jenkins that executes sonnar-runner against all the projects in my repository. This job is independent from code coverage and runs a few times a day.
On the other hand, I want code coverage to happen upon a build (using JaCoCo) and import the reports into Sonar using Ant.
I set up my environment and added the JaCoCo and Sonar plug-ins to the Ant lib directory. When I run the Ant target I can see the results of JaCoCo locally in XML and HTML.
All I want is to upload these results to a remote SonarQube instance. However, after setting up the Sonar properties and adding <sonar:sonar> to my build.xml I realized that Sonar seems to be running static analysis against my project.
How do I set up Ant to only run code coverage for a project?
You can't run only a small portion of the full analysis.
Imagine for a moment that this were possible: Let's say that once an hour a "coverage only" job runs that updates the coverage on my project.
At 8 a.m. I start with a project with 6 LoC and 50% coverage.
At 9 a.m. my project has "6 LoC" and 20% coverage
10a.m.: "6 Loc" and 10% coverage.
Has someone been removing tests? Or adding new, uncovered LoC?
I have no way of knowing because my partial analysis has put the SonarQube project in a bad state.
And this is why there's no way to run only part of the analysis.

Analyzing ant project with sonar ant task and jenkins

I am using the sonar ant task jar for sonar analysis so I setup it in the project-properties.xml and create an Ant build step in jenkins. When I build the job, the analysis works fine and I am able to see the results in the sonarqube server, but in jenkins no link to SonarQube is visible.
Do I really need to install the SonarQube plugin for the link to be visible for the job?
See also how to publish sonar result in jenkins server, or do we have sonar-report jenkins plugin
Normally you let SonarQube handle the code analysis on it's own and not by the Ant task (in the Jenkins job).
The Jenkins plug-in is there to trigger Sonar, to get latest sources (from the SCM) and run the analysis.
The results, are then made available on Sonar, but the Jenkins project, will provide a link to the results.
Workaround
There is (for as far as I know) no way to add a link, to an external generated report. What could be done, is add a simple HTML page, with a link to the Sonar server, to the project. And publish that simple HTML page, with the HTML Publisher Plugin.

Adding jacoco integration tests coverage for Sonar

Background:
I have 2 separate java projects, call them A and B.
Project A is the actual product (war application), with unit-tests. Gradle builds the project and then runs sonar analysis, and I can see the unit-tests coverage in Sonar.
Project B is an integration test for the first project. It is run by Jenkins in a pipeline after building project A and deploying it on an integration-environment. The deployment also involves instrumenting the code so that the jacoco-it report will correlate to Project A's classes.
My question:
How can I add to project A's sonar page, which currently has only unit-tests coverage, the integration tests coverage - as a second step?
The flow I need is:
build project A
run sonar analysis on project A (now it'll show unit-tests coverage on its sonar page)
build project B
run sonar analysis on project B - which will simply add integration-coverage to project A on sonar
It's currently not working. when I run sonar analysis on project B it messes up project A's sonar page, removing the unit-test coverage of A.
The flow that I have now, which is working but I want to change is:
build project A
run sonar analysis on project A
build project B
run jacoco report on project B. this outputs the jacoco-it.exec file into a specific location on my disk
run sonar analysis on project A (it has a setting to take the jacoco-it.exec file from the specified location
now project A's sonar page will show both unit-tests and integration-tests coverage, but step 5 is completely redundant and I want to avoid it.
Any suggestions?
what i will suggest is a bit different:
employ some build automation tool
create a sonar 'checker' job, which doesnt do the full analysis; just the incremental, this should break when add quality gate breaker issues
create a nightly job for your A project with full analysis: -Dsonar.analysis.mode=analysis (note: my experience shows that in this case you must run an incremental prior to this analysis, because in analysis mode sonar accepts new quality gate breaker issues
i don't think integration tests should count into project test coverage, so that might be just left out.
You cannot amend a previous analysis, which is why your 'B' job appears to replace the unit test numbers. What you need to do is generate your integration test coverage report before analysis, and make that report available so that both unit test and integration test numbers can be read in the same analysis.
From a build pipeline standpoint, that could get tricky depending on how you need to structure the jobs, but off-hand it sounds like you need 3 jobs:
build project A
build project B & generate integration test report
job 3 pulls integration test report from job 2, and either rebuilds project A or pulls code, classes, and unit test report from job 1 and performs analysis

Categories

Resources