如何使Maven单元测试代码覆盖工作

问题描述 投票:15回答:2

在Eclipse中,我使用EcLEmma来查看单元测试代码的覆盖范围。哪个工作正常。因此,我尝试使用Maven的JaCoCo插件来查看与Maven构建的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>

在Build部分

        <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

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

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

.... =>长项目路径

java maven unit-testing jacoco
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.