How can I to see the code coverage per each test? - sonarqube

Is it possible to achieve a code coverage per each single test method with JaCoCo and Maven and to see it SonarQube?
I wish to see additionally by which unit test(s) a line or condition has been covered. Instead it shows me only the coverage of all tests combined, in the JaCoCo report and in SonarQube.
The generated jacoco.xml file does not contain any information on tests but only the source classes and methods. I think many years ago at some other place I managed to achieve a per test coverage, but I do not remember the solution.
There is a old blog which says to add a argline to Surefire configuration in Maven POM file: -XX:-UseSplitVerifier. But that does not work with newer Java versions (11 and above) as it turned out.
Does someone know if that is possible with the latest versions of JaCoCo, Maven, JUnit 5, SonarQube and Java?

Related

How can I set-up Maven to create Jacoco coverage for tests for source code another module?

I have a multi-module Maven project. Many of the tests are in the conventional directory (src/test/java) but about 60% are in single module.
By default, Jacoco does not create coverage for those tests.
I'd like the tests in the single module to contribute to coverage.
Nb. We collects coverage in Sonar.
To fix this, set the following configuration item:
<inclNoLocationClasses>true</inclNoLocationClasses>

Integration Test Code Coverage using jacoco and maven

I have a maven test project which tests our dev code. These 2 are different maven projects.
Test project access dev project via maven dependency.
I want to measure integration test code coverage against dev project using jacoco and maven plugin.
Previously I have done it using jacoco and sonar where test code generates jacoco.exec file and then I manually built dev project passing jacoc.exec as arg in jenkins and get code coverage report.
I was not basically looking for 100% automated way of doing this.
Have read couple of articles which uses jacoco with maven but all those uses single project.
Appreciate any help/pointer.
Thanks
The 'dirty trick' here is to accumulate Jacoco coverage reports. As you have different projects there is no legal way to do this as by design maven projects are to be built without direct dependency.
But in real world nothing is ideal:
You point JaCoco report file with fixed path. By the way you can do it relatively to your Maven repository which is pretty clean approach.
You build your projects in 2 phases: first phase build all projects running all possible tests. It's better to have separate JaCoco report files per unit and integration tests. But they should be the same among projects and projects shall be configured to accumulate reports.
Doing second pass you run your mvn sonar:sonar magics. This will bring you 2 sonar projects (as I understand now you have the same picture) but coverage will be much more precise. The key is unified reports calculated on previous stage.
Here is complete enough illustration of this idea and lot of references to more simple projects. And here is another question which illustrates idea.
Hope this helps. I do the same approach for my multi-module projects to get coverage for 'lower layer' modules when doing tests for 'higher layer' ones.

Manage multiple Surefire ReportPaths with Sonarqube/Jenkins

In my company we develope eclipse plugins continuously built by a Jenkins CI Server (with tycho & maven) which also executes the tests (Unit Tests and SWTBot Tests). As a post build action we send the test data (JaCoCo & Surefire reports) via maven to a SonarQube server for analysis. Our sources and tests are each located in an own module.
In src/com.mycompany.projectA/ we have the source from Project A.
In test/com.mycompany.projectA.tests.ut/ we have the unit tests from Project A.
In test/com.mycompany.projectA.tests.swtbot/ we have the swtbot tests from Project A.
In order to allow Sonar to find the Jacoco and Surefire reports for the tests of each source plugin we set the report paths in our pom.xml:
<sonar.jacoco.reportPath>${project.basedir}/../../test/${project.artifactId}.tests.ut/target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.surefire.reportsPath>${project.basedir}/../../test/${project.artifactId}.tests.ut/target/surefire-reports</sonar.surefire.reportsPath>
But this only allows one test per plugin. To also see the SwtBot test coverage in SonarQube, we added the SWTBot tests as integration tests (yeah I know, not very neat...)
<sonar.jacoco.itReportPath>${project.basedir}/../../test/${project.artifactId}.tests.swtbot/target/jacoco.exec</sonar.jacoco.itReportPath>
Like this, we can at least see the unit test coverage as well as the SWTBot test coverage in SonarQube. But as we are only able to set one report path for the surefire reports, those are ignored by Sonar and we can not see how many SWTBot test passed/failed.
Is there a convenient way to add multiple locations of surefire reports to be considered in the analysis?
This is not possible yet, but there's a ticket open for that: https://jira.codehaus.org/browse/SONAR-4101
Feel free to watch it and vote for it.
Maybe you can append the jacoco.exec of each project together and put the integrated jacoco report to Sonar. The following link is a good example to integrate the jacoco report.
http://www.lordofthejars.com/2012/07/jacoco-in-maven-multi-module-projects.html

Maven Multi-Module plus Extrenal Tests plus JaCoCo plus Sonar

On our company we have the several modules on project and each module has several unit tests, but we have system tests that based on classes and not modules. Our system tests use several classes of each module (not all). We cannot calculate intergration coverage and unit coverage of this tests. We want to merge results of system tests to calculate the coverage of all probuct.
Anyone have any idea how we can do this? Anyone can provide any tutorial with examples?
You can find a sample application that reproduces this case here: https://github.com/SonarSource/sonar-examples/tree/master/projects/code-coverage/combined%20ut-it/combined-ut-it-multimodule-maven-jacoco
This should help you.

COBERTURA configuration within SONAR

I am using SONAR for Code Quality checks of my projects. In one project I would like to know the code coverage of a library which is included in the classpath (maven dependency).
Is it possible to configure SONAR (with embedded COBERTURA) to also instrument the specific library for code coverage analysis? As cobertura instruments the bytecode this should be possible but I do not know if it is supported by cobertura (even indepentend from SONAR).
Any hints are welcome.
Regards
Klaus
You would have to set up cobertura(maven target) yourself and import the results(See dynamic analysis)
sonar.dynamicAnalysis=reuseReports
sonar.cobertura.reportPath=PATH_TO_RESULT
But I will not help much:
you would need the src files of the jar to see the coverage, otherwise you would just get % numbers and I'm not even sure sonar will show the extra covered files
the coverage for your whole project will always include the % of the library, so it will go down
It is better to test each project with its own unit tests on its own.

Resources