声纳没有测试覆盖率

时间:2015-08-05 12:11:52

标签: maven sonarqube

我有一个多模块maven项目。我有2个测试。在构建期间运行的测试和sonarqube显示100%测试(2)通过。但报道是空的。为什么?我的另一个简单的maven项目在sonarqube成功上传了覆盖范围。我在两个项目中都以同样的方式进行了测试。

1 个答案:

答案 0 :(得分:1)

可能有更多方法可以做到这一点,但我找到的最佳方法是使用Jacoco maven插件,将其配置为将其代理添加到Surefire执行中(并且,如果使用集成测试,也可以选择,Failsafe)然后将这些文件添加到SonarQube配置中。

对此我的一个很好的解释是here。基本的东西很简单(这里只是为了肯定):

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
        <!-- Sets the VM argument line used when unit tests are run. -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <skipTests>${skip.unit.tests}</skipTests> 
    </configuration>
</plugin>

通过这种方式,您可以配置SonarQube(设置 - &gt; Java)以在/target/coverage-reports/jacoco-ut.exec找到jacoco unittest文件,这足以在之后运行sonar:sonar目标时可靠地获得覆盖率。