Thursday, March 24, 2011

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

 src
 test

 
 
  
   org.sonatype.flexmojos
   flexmojos-maven-plugin
   4.0-beta-5
   true
   
   
    
    com.adobe.flex
    compiler
    4.1.0.16248
    pom
    
   
   
   
    TestSonar.mxml
    C:\FlashPlayer\FlashPlayer.exe
    true
    
	xml 
    
    true
   
  
 

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

    flex
    reuseReports
    target\surefire-reports
    target\coverage\coverage.xml
  

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

7 comments:

  1. 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

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

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

    ReplyDelete
  4. 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!

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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?

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

    Thanks

    ReplyDelete