Reporting Unit Test and Integration Test Coverage In Sonar

If you are familiar with sonar, then the standard setup of the sonar plugin in your maven project will report Unit test coverage using Cobertura.

Sonar can also report Integration Test coverage using Jacoco

http://www.sonarsource.org/measure-coverage-by-integration-tests-with-sonar-updated/

However to report Integration test coverage on Multi-module maven projects, you may need you to tweak your build

Lets take this example

Projects:
Parent
Project A (jar)
Project B (jar)
Project C (Webapp utilising A and B)

Now if you do

mvn clean install sonar:sonar

on the parent project

What happens is the following

clean, install sonar:sonar will get run on project A
clean, install sonar:sonar will get run on project B
clean, install sonar:sonar will get run on project C

This setup may work if you want to report Unit Test coverage, but if you want to report Integration test coverage (where your integration tests get run against the final assembled war built in project C). The above is running sonar analysis too early for project A and B – as its project C that is will run integration test and produce the IT dump file for sonar to analyse.

You will need redo you your build to a multi-step one,

Multi-Step Build

Step 1 , You run mvn clean on the parent project, to delete everying in the target folder (including any Jacoco dump from previous runs )

Step 2 you run mvn install on the parent project, to run your unit tests and integration tests and produce the single Integration Test Jacoco dump file.

Step 3 from the parent, you run mvn sonar:sonar

Why run multi-step, well, it’s so that when you run sonar:sonar in Step 3, when its comes to analysis project A, B, C, sonar can refer to the single jacoco IT dump file which was produced in Step 2.

Details about Step 2

In Step 2, when you run you Integration tests, its good idea to separate Unit Test and Integration Test. maven-sure-fire plugin to run your Unit Test and maven-fail-safe plugn to run your IT tests.

Maven-fail-safe plugin extends the sure-fire-plugin.

The difference between the two plugins
The default convention for Surefire is that Tests follow the pattern **/Test*.java,**/*Test.java or **/*TestCase.java
Main goal is “test”

The default convention for Failsafe is that Tests follow pattern **/IT*.java, **/IT*.java or **/*ITCase.java
Main goal is “integration-test”

Right thats the difference between the two plugins sorted. We can use surefire to run our Unit Tests. and failsafe plugin (or launch external Java process e.g. Tomcat) to run our Integration Test

Now how to we get the failSafe plugin (or other Java process) to generate the IT Jacaco file in step 2 of our build?

If you are using fail-safe-plugin to run your Integration Test.
You’ll need to set the “argLine” config of the plugin to something like this

-javaagent:C:\tools\jacoco-agent.jar=destfile=C:\build\myparentprojectpath\target\itjacoco.exec,append=true,includes=com.mypackage.*

If for example you intend to run your integration tests as part of Tomcat, you can set the above argLine as your “CATALINA_OPTS” environment variable.

Note
One thing to bear in mind, the IT jacoco file is intially 0 bytes when but when the failsafe plugin or your java program ends, you should see the jacoco file being populated.

Super, now when your run your Integration Test in Step 2, you will have an produced an single itjacoco.exec file ready for Step 3 of your build

More about Step 3

Because we’ve already run our unit and integration test in phase 2, we want to avoid sonar having to re-run unit tests and integration test if possible, so we set this property in our maven build

-Dsonar.dynamicAnalysis=reuseReports

When sonar tries to work out the integration test coverage for project a, b, and c it needs to know where the global IT jacoco file is. So we need to pass another property to maven for this.

-Dsonar.jacoco.itReportPath=C:\build\myparentprojectpath\target\itjacoco.exec

Now sonar can report integertion test coverage in your mutli-module project.

Leave a Reply

Your email address will not be published.