How to get setup sonar coverage report for integration tests

Background

Sonar currently reports on the code coverage of unit tests only using the Emma code coverage tool. It is however

desirable to have code coverage for integration and acceptance tests as well. This will help us measure our progress

towards automating all manual tests and improve the efficiency of our test base. 

Jacoco

Jacoco is a code coverage tool that uses a different approach from Emma in to obtain code coverage data. Basically, 

it does bytecode instrumentation on-the-fly using the java agent technology. It works by inserting probes (additional instructions

that record the fact that they have been executed) into the code base accessible to the virtual machine. It is therefore important

to define inclusions and exclusions to restrict jacoco to mifos code.

Getting the tools

  • Download jacoco 
  • Download sonar jacoco plugin 
  • Download sonar 
  • Insert the jacoco plugin in the $SONAR_HOME/extensions/plugin  directory

Doing the business (getting coverage report - integration tests)

  • Modify the pom.xml in $MIFOS_HOME/applicaton directory by adding the following line to maven-surefire-plugin configuration element
<argLine>-javaagent:${jacoco.agent.path}=destfile=${jacoco.file.path},includes=org.mifos.*,excludes=*_javaassist_*</argLine>
  • Run acceptance tests, supply the arguments defined in the maven-surefire-plugin above
mvn -Djacoco.agent.path="/path/to/jacoco/library/jacocoagent.jar" -Djacoco.file.path="/path/to/coverage/dump/jacoco.exec" clean install
  • Run sonar analysis on entire project
mvn -Dsonar.jacoco.itReportPath="/path/to/coverage/dump/jacoco.exec" sonar:sonar -Dsonar.dynamicAnalysis

                 consider using -Dsonar.dynamicAnalysis=reuseReports  to considerably reduce the time taken for code analysis

  • Go to sonar web interface and click on 'configure widgets' to add IT coverage widget 

Miscellaneous 

Jacoco Ant tasks - There exist ant tasks in the jacoco distribution that can be used were using java agent creates problems.

Debugging - Code coverage dumps can be inspected by passing the dump file as an argument to ExecDump.java found in the examples folder in the jacoco distribution. 

References

- [measuring code coverage for integration tests using sonar|http://www.sonarsource.org/measure-code-coverage-by-integration-tests-with-sonar/]

- [Java agent|http://download.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html]

- [Jacoco|http://www.eclemma.org/jacoco]