天天看点

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

大家好,我是神韵,是一个技术&生活博主。出文章目的主要是两个,一是好记忆不如烂笔头,记录总结中提高自己。二是希望我的文章可以帮到大家。欢迎来点赞打卡,你们的行动将是我无限的动力。

本篇主题是:单元测试覆盖率表单生成交付,Jacoco的使用

其它文章链接:

Unit Test 1–什么是单元测试

Unit Test 2–IDEA配置并查看单元覆盖率

Unit Test 3–编写单元测试之前需要了解的单元测试框架Mock

Unit Test 4–自动生成单元测试插件之TestMe与Diffblue

Unit Test 5–编写第一个单元测试

Unit Test 6–单元测试踩过的坑

Unit Test 8–单元测试覆盖率、Sonar和Jenkins-待须…

生成覆盖率表单只需要如下几步

1、加入maven运行插件

<build>
    <finalName>unit test</finalName>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.5</version>
        <configuration>
          <excludes>
            <!-- 不参与unit test -->
            <exclude>**/*App*</exclude>
            <exclude>**/jackson/**</exclude>
            <exclude>**/remote/**</exclude>
            <exclude>**/entity/**</exclude>
          </excludes>
        </configuration>

        <!-- 配置路径,生成jacoco.exec,生成report等-->
        <executions>
          <execution>
            <id>prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
          <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
            <configuration>
              <dataFile>target/jacoco.exec</dataFile>
              <outputDirectory>target/jacoco-ut</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

           

针对不需要进行unit test的类及包用exclude进行一遍过滤

2、执行命令 mvn clean install

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

或者

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

3、install 成功后

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

4、查看target目录

生成jacoco-ut上面我们配置的文件名称

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

5、查看覆盖率

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

打开index.html

Unit Test 7--单元测试覆盖率表单生成交付,Jacoco的使用

本文结束!