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

Advertisements

7 comments

  1. Anonymous · August 18, 2011

    Hi Tony, thanks a lot for this post, it is really helpful. A question about getting the coverage in xml though. Your code segment doesn't work for me and I can only get html output. I'm use 4.0-RC1 of flex-mojos

    Thanks,
    Scott

    Like

  2. Anonymous · August 19, 2011

    Figured it out, the code is just missing an end param tag

    Like

  3. Tony Georgiev · August 19, 2011

    Hey! Thanks for pointing that, seems like this code block does not like the closing param tag, and skips it. I will fix it

    Like

  4. psytranceismyreligion · September 29, 2011

    Hi Tony,

    I've been trying to get this working with the same version of flexmojos, but I get the following error:

    net.sourceforge.cobertura.javancss.parser.ParseException: Encountered ” “< " "< "" at line 1, column 1.
    Was expecting one of:

    “abstract” …
    “class” …
    “enum” …
    “final” …
    “import” …
    “interface” …
    “native” …
    “package” …
    “private” …
    “protected” …
    “public” …
    “static” …
    “strictfp” …
    “synchronized” …
    “transient” …
    “volatile” …
    “;” …
    “@” …
    “@” …
    “@” …
    “@” …
    “package” …

    at net.sourceforge.cobertura.javancss.parser.JavaParser.generateParseException(JavaParser.java:10296)
    at net.sourceforge.cobertura.javancss.parser.JavaParser.jj_consume_token(JavaParser.java:10172)
    at net.sourceforge.cobertura.javancss.parser.JavaParser.CompilationUnit(JavaParser.java:455)
    at net.sourceforge.cobertura.javancss.parser.JavaParser.parse(JavaParser.java:137)
    at net.sourceforge.cobertura.javancss.Javancss._measureSource(Javancss.java:256)
    at net.sourceforge.cobertura.javancss.Javancss._measureRoot(Javancss.java:339)
    at net.sourceforge.cobertura.javancss.Javancss.(Javancss.java:419)
    at net.sourceforge.cobertura.reporting.ComplexityCalculator.getAccumlatedCCNForSource(ComplexityCalculator.java:102)

    What version of the cobertura jar are you using?

    Also, is your patch included in the 0.4 version the Sonar Flex plugin?

    Thanks!

    Like

  5. Tony Georgiev · September 29, 2011

    Hello!
    Yes now 0.4 Sonar Flex plugin supports code coverage. I am also using sonar-cobertura-plugin-2.6

    If you are not building over the sample test project that I have provided, please do so, and tell me what are the results. Just download, unzip the archive and run “mvn clean install sonar:sonar -Pflex”.
    Otherwise, I cannot say what seems to be the problem.

    Like

  6. psytranceismyreligion · September 30, 2011

    Thanks for that Tony! I've found the problem, which was that I copy pasted your sonar properties in the config snippet above. Unfortunately you didn't camelcase your reportpath properties so the FlexCoberturaSeonsor was looking for corverage.xml in /coverage/cobertura/, not /coverage. Once I camelcased these properties it works!

    I've noticed though that CPD doesn't seem to work for me. For example if I copy paste a method in your sample -eg divide and rename it divide2, I still get 0% duplicate lines in my Sonar report. Is this something you've experienced?

    Like

  7. Tony Georgiev · October 18, 2011

    Yeah CPD doesn't work for me either, I will look at it when I can!

    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s