Unit testing with FlexMojos and displaying Cobertura coverage in Sonar

The great thing about running unit tests with FlexMojos 4 is that it can also generate code coverage report. See it here http://www.sonatype.com/people/2010/04/flex-test-coverage-kept-simple-with-flexmojos/

I just love that feature, because in the past you had to use a modified version of the Flex SDK with an external AIR Application to do that, but now it is very simplified.

I also love using Sonar to gather and display statistics for source code. I thought it would be great to build my project and run unit tests with FlexMojos and then reuse the reports created by it and show this information in Sonar. You are able to reuse the surefire reports using the flex plugin for Sonar, but however this was not possible for the cobertura coverage report . So what I did was to browse the code for this plugin and see how Olivier Gaudin and Evgeny Mandrikov, the guys written it, handle the surefire reports. And so I though that it was going to be easy using the code from the cobertura sonar plugin to implement this feature. And somehow I did.

I’ve added this patch in the Jira, so hopefully in the next version it will be included. Until the official version, you can use my modification (see bellow for download)

Here is how to use it. In your pom.xml you would have something like

<build>  
 <sourcedirectory>src</sourcedirectory>  
 <testsourcedirectory>test</testsourcedirectory>  
  
 <plugins>  
   
  <plugin>  
   <groupid>org.sonatype.flexmojos</groupid>  
   <artifactid>flexmojos-maven-plugin</artifactid>  
   <version>4.0-beta-5</version>  
   <extensions>true</extensions>  
     
   <dependencies>  
    <dependency>  
    <groupid>com.adobe.flex</groupid>  
    <artifactid>compiler</artifactid>  
    <version>4.1.0.16248</version>  
    <type>pom</type>  
    </dependency>  
   </dependencies>  
     
   <configuration>  
    <sourcefile>TestSonar.mxml</sourcefile>  
    <flashplayercommand>C:\FlashPlayer\FlashPlayer.exe</flashplayercommand>  
    <coverage>true</coverage>  
    <coveragereportformat>  
    <param>xml <!-- param-->  
    </coveragereportformat>  
    <testfailureignore>true</testfailureignore>  
   </configuration>  
  </plugin>  
 </plugins>  
</build>  

That snippet would build and test your project. Important here is to specify coverage and coverageReportFormat . The coverageReportFormat is xml, so it can be used later on.

And the properties in the pom.xml for the sonar plugin are

<properties>  
    <sonar.language>flex</sonar.language>  
    <sonar.dynamicanalysis>reuseReports</sonar.dynamicanalysis>  
    <sonar.surefire.reportspath>target\surefire-reports</sonar.surefire.reportspath>  
    <sonar.cobertura.reportpath>target\coverage\coverage.xml</sonar.cobertura.reportpath>  
</properties>

I think everything here is understandable, you specify to use the plugin for flex and reuse the generated reports for surefire and cobertura.

See the full pom.xml here

And to build all up run:

mvn clean install sonar:sonar -Pflex

Have in mind that you should use maven 3 for FlexMojos 4

See the result

So if you can’t wait to try this out, get this patched version, if not you could probably wait for an official release, I hope there will be one soon.

Download the patched version of flex plugin for sonar from here. (Again, this is my modification of the original version, it is not the version provided by Codehaus, Sonar)

Download the test Flex project with Unit tests from here