天天看点

Maven项目使用Jacoco生成测试覆盖率报告

pom.xml修改:

(1)导入依赖:

<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.8</version>
</dependency>      

(2)添加插件(在build中):

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <!-- attached to Maven test phase -->
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>      

(3)此段功能待确定,不加也可以生成Jacoco报告:

<reporting>
    <plugins>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <reportSets>
                <reportSet>
                    <reports>
                        <!-- select non-aggregate reports -->
                        <report>report</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>

    </plugins>
</reporting>      

到此处,pom.xml配置已经完成。

在IDEA中的Terminal运行mvn clean install,在target目录下生成site-》Jacoco;其中有index.html,

Maven项目使用Jacoco生成测试覆盖率报告

右击,选择Open in Browser,即可查看报告

Maven项目使用Jacoco生成测试覆盖率报告