Calculating the code coverage of integration tests

In our development structure we use an internally developed integration test system, based on Selenium, to test web application user interfaces. It works great. The Selenium tests run against the test environment (usually JBoss, but it can be any application server) and the tests are run periodically by the continuous integration tool, Jenkins. We can even automatically export test results with test steps to Confluence (our wiki) and link the test cases to their specific use cases.

But there was one thing missing: Calculating the code coverage of the integration tests.

We already used Cobertura for unit test coverage, so I started searching for the possibility of using Cobertura for the integration tests as well. And the conclusion is that it is possible:

  1. First, build your web application, resulting in a .ear or .war file.
  2. Let Cobertura instrument this artifact using the cobertura-instrument command line utility (Cobertura can instrument a .jar or .war, but not .war and .jar files in some .ear file, in this case, extract the .ear file first).
  3. Move the cobertura.ser file generated by cobertura-instrument to the directory where your application server runs from. For me, using JBoss 4, this was the bin-directory.
  4. Start the application server.
  5. Run your Selenium tests against the environment with the instrumented application.
  6. Stop the application server (when using JBoss, be sure jboss.shutdown.forceHalt is set to false, otherwise, the cobertura.ser file might not be written completely).
  7. Use cobertura-report to generate the XML or HTML report.

And just like that, your integration tests coverage report is generated!

I did not manage to use the report in Jenkins because the Cobertura plugin from Jenkins thinks mojo has to run before a coverage report can be generated… I did manage however to use the coverage result in Sonar. Unfortunately, I could not add the integration tests coverage data to the project also containing the unit tests coverage data. That said, I was able to create a different project in Sonar containing the integration tests coverage using Sonar Runner. The sonar-project.properties file I used looked like this:

sonar.projectKey=my:key
sonar.projectName=Integration Tests
sonar.projectVersion=1.0

tests=src/test/java
sources=../project/src/main/java
sonar.dynamicAnalysis=reuseReports
sonar.cobertura.reportPath=../coverage-report/coverage.xml
sonar.skipDesign=true

There are other tools than Cobertura for calculating code coverage, like JaCoCo for example. JaCoCo might collaborate easier with Sonar when calculating integration tests coverage, but I did not try it.

Hopefully this will help you in determining your integration tests code coverage. Feel free to ask questions, share your experiences or comment!

  • Facebook
  • LinkedIn

2 thoughts on “Calculating the code coverage of integration tests

  1. Well, the reason why I do selenium testing is the integration between html, javascript, java, velocity and whichever other technologies my application consists of. If I cared most about the coverage of java classes, I’d run different type of tests. Or does cobertura take care of javascipt coverage too ?

  2. Of course, integration tests are useful to test the integration of all used technologies. Calculating the code coverage of Javascript too would be nice, but had no priority in the particular project for which I started calculating the code coverage. Also, the project does not contains not much Javascript code.

    The reason why we started calculating the code coverage was mostly to find dead code and to get any idea about how much we have tested the application. We did not have any clue, cause the project came from a third-party software company without any integration tests (or even unit tests).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>