Sonar Java - Is there a way to enforce unit test coverage? - java

Currently I have Sonar setup to show unit test coverage which works great. Is there a way to enforce a certain percentage of unit test coverage for a project? For example if the coverage drops below 50%, I want it to be a sonar violation and for my build to fail.

Yes, it is possible. For example, if you use jenkins, you can configure and step in which you can choose the minimum coverage(JaCoCo Plugin).
In the jenkins job, you can pass the unit test, and take the coverage report of jacoco, and then, a step with the sonar plugin, but if the coverage is less than for instance 50%, the jenkins will show your build as failure.

You need to install this plugin Build Breaker the only purpose of this plugin is to break the build when new alerts raise in the analysis.

Related

How do I get my Jacoco Test Coverage Report to open automatically when jacocoTestCoverageVerification fails?

I have configured Jacoco successfully in my project, but I'd like to have my test coverage report open automatically when the test coverage verifications rules I set up fail.
Would I have to use gradle tasks to do that, or is there an out of the box way to accomplish that?
I have tried finding a way to do so, but I could not find a post anywhere.
Thanks!

Verify jacoco covarage tests using power mockito without mvn command

Is there a way to verify the coverage of jacoco in eclipse without run mvn test command? I'm using power mock, so Eclemma does not work in my case.
Try a plugin e.g. EclEmma
https://www.eclemma.org/
Fast develop/test cycle: Launches from within the workbench like JUnit test runs can directly be analyzed for code coverage.
Rich coverage analysis: Coverage results are immediately summarized and highlighted in the Java source code editors.
Non-invasive: EclEmma does not require modifying your projects or performing any other setup.

Sonar cube not showing the test coverage percentage for Java project properly

I have a spring-based java project and I have my tests placed in /src/test/java folder. I tried to run the Sonar cube locally via
docker run -d --name sonarqube -p 9000:9000 sonarqube
in order to analyze the percentage of the test coverage. The test coverage shows 0% even though I have many tests in my project. And the Number of Unit tests count shows as 8 though there are more than that. Here is the screenshot
And here is my general Settings
Does anyone has any idea how to configure Sonar cube to reflect the test coverage?
I faced the same situation when trying to use Sonar with Docker.
What worked for me was:
Make sure you have run your tests [You can use mvn test]
Make sure JaCoCo has generated the coverage report [In my case reports were generated in folder target\jacoco-report]
Set the following property pointing to your XML report
Sonar property to be set
Sonarqube and sonar scanner do not provide tooling to generate code coverage reports. This should be done with tools like jacoco for java or opencover for .NET.
You can then add the output of jacoco to the sonar scanner with the sonar.coverage.jacoco.xmlReportPaths property.
so you will basically need the following steps for your analysis:
Sonar scanner begin
Restore packages
Build application
Use code coverage tool to test and calculate the coverage for your app
Sonar scanner end

Sonar false overall coverage (jacoco)

I'm running Jacoco and Sonar on multimodule Java8 project.
I have unit tests in each of the modules and to save resources I collect all 'integration tests' into one "integration-tests-runner" and run them all there (wrapping them with before and after tests).
When measuring coverage UT generates exec file per module target/jacoco-ut.exec, while the IT generates one exec file: /target/jacoco-it.exec.
When I run sonar I reuse those exec files, giving path to the jacoco-it.exec.
I get a very weird image:
How can it be that overall coverage is lower?
I found the problem and the solution.
From Sonar website I see this:
By default, when no coverage report is found, the JaCoCo plugin will
not set any value for coverage metric. This behaviour can be
overriden to force coverage to 0% in case of a lack of report by
setting the following property :
sonar.jacoco.reportMissing.force.zero=true
This means that UT analysis was skipped for modules without any tests.
Since I've set the sonar.jacoco.itReportPath from parent pom then all modules got analyzed for integration tests coverage, and overall coverage.
Bottom line: setting the property sonar.jacoco.reportMissing.force.zero=true from parent pom fixed the numbers.
Why is that weird? The unit and integration tests execute code and certain executed code chunks overlap. With other words, the code which is covered by the unit and integration tests is not disjoint, thus you cannot simply add them up.

Sonar how to run unit test successfully

I have a project built with maven and I recently integrated Sonar... It is really easy to configure Sonar to analyze you're project but I couldn't configure it to run my project unit test also. I tried something with Jacoco but I get some Seam error and all the other tests are skipped. By the way I'm using TestNG to run tests manually.
You can use the relevant Analysis Parameters of sonar to reuse the test reports from your earlier run. You would set sonar.dynamicAnalysis property to reuseReports and specify the location of the reports in sonar.jacoco.reportPath or sonar.surefire.reportPath based on how you run the tests.
By the way, mvn sonar:sonar invokes maven's test goal, which runs unit tests as part of the analysis. So ideally if your maven can run unit tests, sonar should be able to run them.
Sonar cannot run tests, it can only analyze testing reports.
You can run yourself JUnit ( using Maven or Ant for exemple ) and push reports to Sonar (try Sonar's Maven plugin for that)
or you can give yourself a build factory (try hudson for exemple) and plug it to sonar.

Categories