Jacoco 生成单元测试覆盖率

1. 引入Jacoco插件和Maven Site插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.7.1</version>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <configuration>
        <excludes>
            <!-- excludes 配置要跳过单元测试的类,一般是实体类或Mybatis Mapper类 -->
            <exclude>**/mapper/*</exclude>
            <exclude>**/model/*</exclude>
            <exclude>**/entity/*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2. 引入 Jacoco-report 插件

<!-- 在 pom.xml 的 project 根元素下建立 reporting 元素 -->
<reporting>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
    </plugins>
</reporting>

3. 命令行下运行 mvn test

$ mvn clean test

如果要使失败的测试不影响测试报告的生成,使用

$ mvn clean test -Dmaven.test.failure.ignore=true

生成的测试报告位于 /target/site/jacoco 目录下

posted @ 2022-12-03 18:48  飞鸟_Asuka  阅读(143)  评论(0编辑  收藏  举报