【问题标题】:How to make code coverage from FlexUnit work with Sonar?如何使 FlexUnit 的代码覆盖率与 Sonar 一起使用?
【发布时间】:2012-09-10 14:30:51
【问题描述】:

情况

我试图让 Sonar 显示由 FlexUnit 从使用 Flex-Mojos 的 Maven 构建作业生成的代码覆盖率报告,但我没有任何运气 - 我得到的只是一个令人沮丧的“-” .


构建输出

结果是仪表板总是显示这个(左列): (不,单元测试不会运行超过 90 分钟,而是 16 秒;不知道这里发生了什么)

Sonar 相关的控制台输出是这样的: 所以一切似乎都运行良好(除了 Cobertura 之外没有找不到文件的错误,我似乎无法以任何方式摆脱它,没有解析异常等)。


构建设置

用于构建项目的pom.xml 如下所示:

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

我尝试了几种运行 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(test-run 目标执行期间,单元测试无法运行并出现 NullPointerException)。


有没有办法在 Sonar 仪表板中显示覆盖结果?我是否需要额外的插件(Emma、Clover 等)才能显示标准 Surefire 报告的覆盖范围?是否存在阻止此功能的已知问题?我做错了吗?


更新

我尝试使用 Sonar-Runner 运行 Sonar。有趣的是,仪表板随后完全删除了代码覆盖率小部件。检查运行器的控制台输出表明运行器没有执行FlexSurefireSensor(这是sonar:sonar Maven 目标执行的):

sonar-project.properties 文件包含:

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

我使用mvn clean install 运行它,然后是sonar-runner。

【问题讨论】:

    标签: maven sonarqube flexunit flexunit4


    【解决方案1】:

    要获得代码覆盖率,您需要添加以下属性:

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

    (或任何覆盖文件的路径)。

    这记录在the plugin home page。

    【讨论】:

      【解决方案2】:

      我认为您在 POM 中缺少的属性是 sonar.dynamicAnalysis:

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

      Flex plugin documentation 描述了如何启用单元测试和代码覆盖率报告。它还建议使用 Java Runner,但它应该在功能上类似于 Maven 插件启动器。

      【讨论】:

      • 我已经为sonar.dynamicAnalysis 尝试过此设置,并使用mvn clean install 运行它(构建项目并运行测试),然后使用mvn sonar:sonar(收集Sonar 仪表板的数据)。没用,仪表板仍然显示:“代码覆盖率:-”。由于我试图让 Sonar 运行 UT,并且根据文档 dynamicAnalysis 默认为 true,因此我稍后将属性排除在外。
      猜你喜欢
      • 2012-06-30
      • 2023-03-25
      • 2012-06-11
      • 2012-12-22
      • 2014-03-26
      • 1970-01-01
      • 2014-02-16
      • 2012-10-16
      相关资源
      最近更新 更多