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

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>

Related

SonarQube coverage on multi-modules project

I have a multi-modules project in SonarQube. One of the modules did not have any unit-test so far. Now that I added a unit-test in this module, the overall coverage of the project decreased! Does it mean that when a module has no test, it is not included at all in the global coverage calculation?
You have correctly deduced the situation: If there's no coverage at all, it's excluded from the calculations by default. On a language-by-language basis (available in Java) "force coverage to zero" functionality is available.

Merge test code coverage in Teamcity

We are running unit tests and acceptance tests in Teamcity, both for Java for the same project. Both generate a test coverage report using JaCoCo.
Is there a way to merge the test coverage reports, so that we can see what code is not covered by either suite?
The coverage report is already published as a hidden artifact after each build. It would need to be imported to the acceptance tests step and referenced by maven, as of this:
http://www.eclemma.org/jacoco/trunk/doc/merge-mojo.html

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.

How to get a unified coverage report with Cobertura + Sonar + Multi-module maven project where the reactor is not the parent POM?

We have a setup where we have a multi module maven project, where the reactor POM is not the parent of the individual module POMs - and we can't change that. We're using Cobertura, as we use PowerMock for our unit tests. (we were using JaCoCo before, and that supported generating a single report, but it can't report code coverage for PowerMock enabled unit tests, so we had to swap it out.)
Now, we were wondering: how do we get a single (unified) test coverage report for the entire project in Sonar when we use Cobertura?

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

Resources