Mittwoch, 4. Juni 2008

Measure code coverage of junit tests in OSGi applications

I'm currently looking for a tool which supports us to check code coverage
of JUnit tests for out OSGi application.
Since OSGi modifies the class loading, most of the tools available don't work
in our environment.

Marc R. Hoffmann describes a basic approach for solving this problem on
the eclemma research site.

I needed these steps for getting it work:

  • Create a jar file from the project available from eclemmas svn and put this file into a directory /opt/eclemma

  • Add this two lines to the junit task

    <sysproperty key="osgi.framework.extensions" value="com.mountainminds.eclemma.osgihook"/>
    <sysproperty key="osgi.frameworkClassPath" value="../com.mountainminds"/>

  • Add emma.jar (from the emma homepage) and /opt/ecleamm/com.mountainminds.eclemma.osgihook_2.0.0.200806031327.jar to the junit classpath


After done so, I got a directory coverage-[timestamp] for each test run. This directory holds a file coverage.es which can further be processed by the emma ant merge and report tasks to create a html, text or xml report.

When using OSGiUnit it is also needed to add tbe entry

<bundle jar="file:///opt/eclemma/com.mountainminds.eclemma.osgihook_2.0.0.200806031327.jar" />
as first bundle to your testcases.xml file.

The only problem I found is, that the coverage report shows only the functions and the number of blocks covered, but does not show the sourcecode lines.

Links


EMMA Homepage
EMMA Eclipse Plugin
Using EMMA in OSGi applications
OSGiUnit

Update 2008/06/05



I added all needed jar files and an example build file to the OSGiUnit project, for details see the wiki entry.