How to make code coverage from FlexUnit work with Sonar?

1.4k Views Asked by At

Situation

I'm trying to get Sonar display the code coverage reports generated by FlexUnit from a Maven build job using Flex-Mojos but I'm not having any luck - all I ever get is a frustrating "-".


Build output

The result is that the dashboard always shows this (the left column): Sonar project dashboard (no, the unit tests don't run for over 90 minutes but rather 16 seconds; don't know what's off here)

The Sonar related console output is this: Maven console output So everything seems to work fine (no file-not-found errors other than the Cobertura one which I can't seem to get rid of in any way, no parse exceptions, etc).


Build setup

The pom.xml used for building the project looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>foo</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.3</version>
    </parent>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>swc</packaging>
    <name>Bar</name>
    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito</artifactId>
            <version>1.4M5</version>
            <type>swc</type>
            <scope>external</scope>
        </dependency>
        <dependency>
            <groupId>com.adobe.flexunit</groupId>
            <artifactId>flexunit</artifactId>
            <version>4.1.0-8</version>
            <classifier>flex</classifier>
            <type>swc</type>
            <scope>external</scope>
        </dependency>
    </dependencies>
    <build>
        <testSourceDirectory>src/test/flash</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.sonatype.flexmojos</groupId>
                <artifactId>flexmojos-maven-plugin</artifactId>
                <version>4.2-beta</version>
                <configuration>
                    <coverage>true</coverage>
                    <debug>false</debug>
                    <optimize>true</optimize>
                    <omitTraceStatements>true</omitTraceStatements>
                    <verboseStacktraces>false</verboseStacktraces>
                    <defines>
                        <property>
                            <name>CONFIG::BUILD</name>
                            <value>"${project.version}"</value>
                        </property>
                    </defines>
                </configuration>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>asdoc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>${project.build.directory}/asdoc/tempdita</directory>
                <targetPath>docs</targetPath>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <excludes>
                    <exclude>ASDoc_Config.xml</exclude>
                    <exclude>overviews.xml</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
    <properties>
        <sonar.language>flex</sonar.language>
        <sonar.sources>src/main/flash</sonar.sources>
        <sonar.tests>src/test/flash</sonar.tests>
        <sonar.surefire.reportsPath>target/surefire-reports</sonar.surefire.reportsPath>
    </properties>
</project>

I've tried several ways to run Sonar:

  1. dynamicAnalysis=reuseReports + mvn clean install + mvn sonar:sonar

  2. dynamicAnalysis=true + mvn clean install sonar:sonar -Dmaven.test.failure.ignore=true

  3. dynamicAnalysis=true + mvn clean install -DskipTests=true + mvn sonar:sonar (<-- doesn't work: for some reason in this scenario the unit tests fail to run with a NullPointerException during execution of Flex-Mojos's test-run goal).


Is there a way to make displaying the coverage results in the Sonar dashboard work? Do I need additional plugins for that (Emma, Clover, whatever) to get the coverage from the standard Surefire reports to show up? Is there a known issue that prevents this from working? Am I doing something wrong?


Update

I've tried running Sonar with the Sonar-Runner. Interestingly, the dashboard then completely drops the code coverage widget. Checking the console output of the runner shows that the runner doesn't execute the FlexSurefireSensor (which the sonar:sonar Maven goal does):

Console output for sonar-runner

The sonar-project.properties file contains:

sonar.projectKey=foo:bar
sonar.projectName=Bar
sonar.projectVersion=0.1.0-SNAPSHOT

sonar.language=flex
sources=src/main/flash
tests=src/test/flash

sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=target/surefire-reports

I'm running it with mvn clean install followed by sonar-runner.

2

There are 2 best solutions below

1
On

I think the property you are missing in your POM is sonar.dynamicAnalysis:

<properties>
    ..
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.surefire.reportsPath>target/surefire-reports</sonar.surefire.reportsPath>
</properties>

The Flex plugin documentation describes how to enable Unit test and code coverage reporting. It also recommends using the Java Runner, but it should be functionally similar to the Maven plugin launcher.

0
On

To get code coverage, you need to add the following property:

<sonar.cobertura.reportPath>target/site/coverage-cobertura/coverage.xml</sonar.cobertura.reportPath>

(or whatever the path to your coverage file is).

This is documented on the plugin home page.