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

时间:2016-03-24 11:46:37

标签: java maven unit-testing jacoco

在Eclipse中,我使用EcLEmma来查看单元测试代码的覆盖范围。哪个工作正常。 因此,我尝试使用Maven的JaCoCo插件来查看与Maven构建的Surefire相同的报告,甚至更好,具有特定的配置文件或站点周期。没有成功。这里所有建议的解决方案对我都不起作用。

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

[编辑] 更具体地说为什么jacoco为我失败了....因为我总是这样 由于缺少执行数据而跳过JaCoCo执行

来自pom 在属性

var gulp = require('gulp');
require('my-common-tasks')(gulp);

gulp.task('sub-task', [ 'common-task' ], function() { });

在构建部分

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

是我的最后一次尝试,但是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

.... =&gt;漫长的项目路径

2 个答案:

答案 0 :(得分:21)

感谢user3732793

Personnaly,我只需要将其添加到我的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

下获得了HTML报告

答案 1 :(得分:6)

在阅读提供示例poms jacoco documentation

的文档后,一如既往地解决方案很容易

此个人资料

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

这在构建部分

        <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

感谢