Blogroll

Problem

The past weekend I’ve been struggling to finalize a Maven build that would execute unit tests and integration tests and then aggregate the code coverage reports from these 2 tests.

The task seemed pretty easy at the beginning, especially after checking out the new integration testing tools built in Sling. The sample project is quite straight forward, easy to understand and easy to get started with integration tests.

However, there were a few differences in the way I wanted to run the tests. In Apache Sling integration tests are written in separate projects. In my case, I wanted to create the infrastructure to write and execute the integration tests as part of each OSGI bundle; in this way I would be able to generate a more complete code coverage report, aggregating the results from unit tests with the results from the integration tests.

Solution

Create a special maven profile to execute the unit test : “integration-tests”

Get the code from the integration-tests Sling project sample and integrate it into the profile.

Configure the additional sling bundles that need to be deployed during the integration tests.

Configure the bundle to be tested so that it gets deployed on Sling, along with the other bundles.

Separate executing of unit-tests from integration-tests

Add Cobertura build step in order to compute the code coverage

Aggregate the unit-test reports with the integration-tests reports into a unified code coverage report.

Step by step details

Step 1. Add the new maven profile

<profile><id>integration-tests</id></profile>

Step 2. Get the Apache Sling testing projects

svn co http://svn.apache.org/repos/asf/sling/trunk/testing/

Step 3. Copy the build directives and the dependencies from the integration-tests project, into the “integration-tests” profile you’ve created at step 1.
Then update the configuration for maven-failsafe-plugin to include the bundles you need for the integration tests. I.e.

Step 5. Separate executing of unit-tests from integration-tests. There are several ways to achieve this, what I’m describing here is just what I’ve chosen.
Update maven folder structure:

-src
- integration-test
- main
- test

Use JUnit4 Test @Category(IntegrationTest.class) annotation to differentiate the integration tests from the unit tests. Even though they dwell in different directories, it made it easier for me to configure Maven build in this way.

Step 6. Add Cobertura build step in order to compute the code coverage. This step was a bit tricky, b/c I had to compile the Cobertura JAR for OSGI; but thanks to the good examples from Apache Felix, it was easy to get it done. The Cobertura dependency needs to be added to the list of additional bundles to be installed during integration tests.

Step 7. Aggregate the unit-test reports with the integration-tests reports into a unified code coverage report.
For unit tests, there is a small trick to unified the reports. Make sure to dump reports into the same place and they get merged. To do this, update maven-failsafe-plugin configuration:

<!-- show IT results together with the unit tests results --><reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>

For integration tests, I had to use an ant task from Cobertura to generate the reports:

1 Comment

rajeev Said:

I tried exactly the same approach as described here, but when the cobertura report generated, they had only given me 0% coverage for the test i ran. The test was available in OSGI bundle and it was executed remotely when i ran “mvn clean install cobertura:cobertura”.