Freddy Mallet posted a comment on my last post about Sonar. Sonar (http://sonar.codehaus.org/) is an all-in-one code quality tracker. So, based on maven (and a set of plug-ins), it collects data about your code, tests … and assembles reports on one web site with useful metrics (efficiency, usability…).
I tested today and was really impressed. But, my code coverage metrics was desperately 0 ☹
After some web searches, I found the Cobertura maven plug-in (http://mojo.codehaus.org/cobertura-maven-plugin/) collecting code coverage during test execution thanks to the Cobertura framework (http://cobertura.sourceforge.net/). However, as my systems and applications are tested in an OSGi container with junit4osgi, I investigated how to collect the code coverage of tests executed with juni4osgi.
So, first it is possible after a very small hack of junit4osgi to manage cobertura packages.
Collecting the code coverage is a three-step process:
A] Creates a bundle with instrumented classes
First you need to select ONE project under test. You will collect metrics on this project. So you want several projects, you need to execute the process several times (one per project).
Once selected, edit the pom file to ignore the ‘net.sourceforge.cobertura.* packages such as in:
Then, execute the following command to create the instrumented bundle:
org.osgi.framework;version=1.3,
org.osgi.service.cm,
org.osgi.service.log,
!net.sourceforge.cobertura.*
mvn clean install cobertura:instrument package installThis command creates the bundle with the Cobertura instrumented code, and installs it in the local maven repository. Moreover, it creates a file containing code metadata in the target/cobertura folder (cobertura.ser).
B] Test execution
Once the bundle is created, you need to execute the test. The code coverage is computed during this execution. So to launch the execution, go in your integration-test project and execute the following command:
mvn clean integration-testThe ‘net.sourceforge.cobertura.datafile’ property specified the location of the cobertura.ser file (created during the Cobertura code instrumentation).
-Dnet.sourceforge.cobertura.datafile
=/path/to/your/tested/project/target/cobertura/cobertura.ser
Tests are executed normally… But when maven is stopping, two messages appear:
Cobertura: Loaded information on 81 classes.This means that the metrics were correctly collected.
Cobertura: Saved information on 81 classes.
C] Generation of the report and analysis of the code coverage
Once collected, go back to your tested project. To generate the report, just launch the following command:
mvn cobertura:coberturaThe report is generated in the target/site/cobertura/ folder. Here is an example of generated report. You can also check your code coverage with:
mvn cobertura:checkThis doesn’t create the report but just gives an idea of the code coverage. You can use the cobertura plug-in configuration to adapt the checking or the report to your requirements.
That’s it.
4 comments:
Hello Clement, I've added iPOJO to NEMO our Sonar public instance (dedicated to weekly analyze Open Source projects) and will do my best to display code coverage information on iPOJO project sooner or later :-).
Thanks you very much Freddy. It's very interesting and I began to fix some issues :-)
How I can know from which tools and rules comes a violation. For instance, all my field names violate the convention, because my convention is different (all fields begins with m_).
If I configure the plug-in correctly (so by specifying my convention), can I avoid those violations ?
But, really, Sonar is wonderful.
Hey,
I was wondering if it is possible to run the test cases without using mvn. From other environment.Or may be deploying these jar in some server say springdm server,run the test cases and then see the coverage report.In this case how to specify the path for cobertura.ser.
Thanks in advance.
Pratik P. Mumbbaikar
Hi,
Not sure it is easily possible. The approach would be to add corbertura packages bootdelegated, and to configure the cobertura.ser location using a system property. However, I never did that. You can llok how it's done in junit4osgi, it should be reproducible.
Clement
Post a Comment