如何使Maven单元测试代码覆盖率生效

32

我在Eclipse中使用EcLEmma查看单元测试代码覆盖率,效果很好。因此,我尝试使用Maven的JaCoCo插件来查看具有相同报告的Surefire,甚至更好的是,在特定配置文件或站点周期中。但没有成功。这里提供的所有解决方案都对我无效。

如何以最佳方式获取单元测试代码覆盖报告(使用surefire)?

[编辑]更加具体地说,为什么Jacoco对我失败了...因为我总是得到“由于缺少执行数据而跳过JaCoCo执行”的信息。

从POM中的属性

    <jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
    <jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>

在“构建”部分

        <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>jacoco-maven-plugin</artifactId>
                                    <versionRange>${jacoco.version}</versionRange>
                                    <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>${jacoco.ut.execution.data.file}</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>${jacoco.ut.execution.data.file}</dataFile>
                                                <!-- Sets the output directory for the code coverage report. -->
                                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                                            </configuration>
                                        </execution>
                                        <!-- Prepares the property pointing to the JaCoCo runtime agent 
                                            which is passed as VM argument when Maven the Failsafe plugin is executed. -->
                                        <execution>
                                            <id>pre-integration-test</id>
                                            <phase>pre-integration-test</phase>
                                            <goals>
                                                <goal>prepare-agent</goal>
                                            </goals>
                                            <configuration>
                                                <!-- Sets the path to the file which contains the execution 
                                                    data. -->
                                                <destFile>${jacoco.it.execution.data.file}</destFile>
                                                <!-- Sets the name of the property containing the settings for 
                                                    JaCoCo runtime agent. -->
                                                <propertyName>failsafeArgLine</propertyName>
                                            </configuration>
                                        </execution>
                                        <!-- Ensures that the code coverage report for integration tests 
                                            after integration tests have been run. -->
                                        <execution>
                                            <id>post-integration-test</id>
                                            <phase>post-integration-test</phase>
                                            <goals>
                                                <goal>report</goal>
                                            </goals>
                                            <configuration>
                                                <!-- Sets the path to the file which contains the execution 
                                                    data. -->
                                                <dataFile>${jacoco.it.execution.data.file}</dataFile>
                                                <!-- Sets the output directory for the code coverage report. -->
                                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                                            </configuration>
                                        </execution>
                                    </executions>
                                </pluginExecutionFilter>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

这是我最后一次尝试,但pom文件变得越来越大却没有任何结果。

以下内容出错:

配置报告插件org.apache.maven.plugins:maven-jxr-plugin:2.3

配置报告插件org.jacoco:jacoco-maven-plugin:0.7.5.201505241946

由于丢失执行数据文件......\target\jacoco.exec,跳过JaCoCo执行

由于缺少执行数据文件......\target\jacoco-it.exec,跳过JaCoCo执行

.... => 长项目路径


你使用过 jacoco-maven-plugin 吗?请解释一下你所做的和尝试的,然后我们可能能够提供帮助。 - Rob Audenaerde
已将Jacoco的POM片段添加到问题中。 - user3732793
在运行构建时添加您收到的错误消息。根据您所说的,我猜测是执行数据文件的路径。除此之外,如果没有构建的错误消息,我们无法提供更多帮助。 - Jorge Campos
抱歉,我也加上了那个。 - user3732793
你为什么把这个放在Eclipse生命周期映射插件中?你是在Eclipse内部运行Maven还是从命令行运行? - tdrury
这只是让它运行的众多尝试之一......我尝试在控制台和Eclipse中运行它。 - user3732793
2个回答

49

感谢 user3732793

就我个人而言,我只需要将这个添加到我的 pom.xml 文件中。

<project>
...

    <dependencies>
        ...
    </dependencies>

    <build>
        <plugins>
            ...
            <!-- Code Coverage report generation -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-code-coverage-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

然后我在奔跑

mvn test

我得到的HTML报告位于./target/site/jacoco/*.html下。


有了这个配置,SonarQube扫描器可以在不进行任何其他设置的情况下找到xml报告。最新的插件版本可在https://mvnrepository.com/artifact/org.jacoco/jacoco-maven-plugin找到。 - Pino

10

阅读提供示例poms的文档后,解决方案通常很容易理解。请参考jacoco文档

这个配置文件:

    <profile>
        <id>test</id>
        <properties>
            <env>test</env>
            <gebEnv>test</gebEnv>
            <jacoco.skip>false</jacoco.skip>
            <maven.test.skip>false</maven.test.skip>
            <skip.unit.tests>false</skip.unit.tests>
        </properties>
    </profile>

build 部分中:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

还有在 报告 部分中:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
        </plugin>

比这更好的:

mvn clean install site -P test

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接