如何使 Maven 单元测试代码覆盖率发挥作用

2022-09-01 14:06:36

在Eclipse中,我使用EcLEmma来查看单元测试代码的覆盖率。这很好。因此,我试图使用Maven的JaCoCo插件来查看与Maven构建的Surefire相同的报告,或者更好的是,具有一定的配置文件,或者在站点周期中。没有成功。这里所有建议的解决方案都不适合我。

获取单元测试代码覆盖率报告(肯定)的最佳方法是什么?

[编辑]更具体地说,为什么雅各对我失败了....因为我总是因为缺少执行数据而跳过JaCoCo执行

从物业中的绒球

    <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变得越来越大,没有任何结果

它失败

configure report plugin org.apache.maven.plugins:maven-jxr-plugin:2.3

configure report plugin org.jacoco:jacoco-maven-plugin:0.7.5.201505241946

由于缺少执行数据文件而跳过 JaCoCo 执行:......\target\jacoco.exec

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

=>长项目路径


答案 1

感谢用户3732793

就个人而言,我只需要将其添加到我的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

并且我在./target/site/jacoco/*.html


答案 2

与往常一样,在阅读提供示例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>

本节中的内容:reporting

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

比这一切做:

mvn clean install site -P test

推荐