美文网首页
教你使用 Jacoco 统计服务端代码覆盖率

教你使用 Jacoco 统计服务端代码覆盖率

作者: AirPython | 来源:发表于2020-08-03 22:17 被阅读0次

    1. 前言

    前面有一篇 文章 使用 Python + Coverage 来统计测试用例的代码覆盖率

    Jacoco 针对 Java 语言的一款开源的覆盖率工具,可以嵌入到 Maven、Gradle 中,提供多种尺度的覆盖率计数器,比如:类覆盖、行覆盖、分支覆盖等

    本篇将聊聊服务端代码的覆盖率统计,以 Spring Boot 项目为例,使用 Jacoco + junit 来统计服务端的代码覆盖率

    2. 准备

    首先使用 IDEA 创建一个 Spring Boot 项目( Maven ),以之前 构建 RESTFul API 的项目 代码为基础

    image

    然后,配置 pom.xml 文件,为当前项目新增 jacoco 依赖 JAR 包

    <!--pom.xml-->
    <!--jacoco依赖-->
    <dependency>
         <groupId>org.jacoco</groupId>
         <artifactId>jacoco-maven-plugin</artifactId>
         <version>0.8.5</version>
    </dependency>
    

    jacoco 版本可以参考:

    https://www.eclemma.org/jacoco/index.html

    接着,配置 Jacoco 插件及相关的 goal,使用 includes 和 excludes 两个关键字设定包含或排除的类路径

    比如:这里只统计 com.xingag.api.service 下面的类

    <!--pom.xml-->
    <!--配置Jacoco插件信息-->
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.5</version>
        <configuration>
            <includes>
               <!--只包含com/xingag/api/service/下面的类-->
               com/xingag/api/service/*
            </includes>
        </configuration>
    
        <executions>
             <execution>
                 <id>prepare-agent</id>
                 <goals>
                      <goal>prepare-agent</goal>
                 </goals>
             </execution>
    
             <execution>
                 <id>check</id>
                 <goals>
                      <goal>check</goal>
                 </goals>
              </execution>
    
              <execution>
                 <id>report</id>
                 <phase>prepare-package</phase>
                 <goals>
                     <goal>report</goal>
                 </goals>
              </execution>
        </executions>
     </plugin>
    

    最后,点击右上角的 Maven 同步,下载依赖并配置项目

    3. 实战一下

    首先,编写一段简单的被测代码,根据考试成绩返回不同的结果

    //ScoreServiceImpl.java
    package com.xingag.api.service;
    //被测代码
    
    public class ScoreServiceImpl {
    
        public String getScoreLevel(int score) {
            String result;
            if (score > 90) {
                result = "优秀";
            } else if (score < 90 && score >= 75) {
                result = "良好";
            } else if (score >= 60) {
                result = "合格";
            } else if (score > 40) {
                result = "不合格";
            } else if (score >= 0) {
                result = "差";
            } else {
                result = "成绩格式不正确";
            }
            return result;
        }
    }
    

    然后,在 test 测试文件夹内,编写测试类和单元测试方法@RunWith(SpringRunner.class) 、@SpringBootTest 用于注解测试类,表明当前类作为一个测试类处理

    //ScoreTests
    //测试类
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ScoreTests {
        ...
    }
    

    接着,在测试类中以 Junit 的 @Test 注解定义 3 个单元测试方法

    //ScoreTests.java
    //定义测试方法
    //成绩优秀
    @Test
    public void testLevelA() {
       Assert.assertEquals(RESULT_LEVEL[0], scoreService.getScoreLevel(95));
    }
    
    //成绩良好
    @Test
    public void testLevelB() {
       Assert.assertEquals(RESULT_LEVEL[1], scoreService.getScoreLevel(80));
    }
    
    //成绩及格
    @Test
    public void testLevelC() {
       Assert.assertEquals(RESULT_LEVEL[2], scoreService.getScoreLevel(70));
    }
    

    最后,在 Terminal 指向项目根目录,输入 mvn test jacoco:report 命令生成代码覆盖率报告代码覆盖率报告目录在:./target/site/jacoco

    image

    用浏览器打开统计报告文件夹中的 index.html 文件,可以很直观的查看单元测试覆盖率及对应测试类的具体覆盖范围

    image

    4. 最后

    上面只是通过一个简单的例子展示了 Jacoco 统计单元测试代码覆盖率的过程

    实际项目中,代码覆盖率只能为单元测试提供一个参考,考虑到开发效率,不能一味地追求高覆盖率;事实上,高覆盖率不一定能保证没有缺陷

    我已经将文中全部源码上传到公众号后台,关注公众号「 AirPython 」后回复「 jacoco 」即可获得全部源码

    如果你觉得文章还不错,请大家 点赞、分享、留言下,因为这将是我持续输出更多优质文章的最强动力!

    推荐阅读
    聊聊 Python 代码覆盖率工具 - Coverage

    教你 10 分钟构建一套 RESTful API 服务

    行为驱动开发:一篇文章带你用 Python 玩转 BDD

    相关文章

      网友评论

          本文标题:教你使用 Jacoco 统计服务端代码覆盖率

          本文链接:https://www.haomeiwen.com/subject/myabrktx.html