jacoco使用示例与常用配置说明

基本概念

有时候,对于一些重要的项目或者重点类,我们希望重点测试,但是我们怎么评估测试质量呢?

这个时候,或许就需要jacoco了。

接下来,我们就来了解一下jacoco的基本概念与使用。

以及在某些条件不允许的情况下,我们如何跳过某些类,再结合mvn参数,让我们的单元测试报告看起来不那么乱。

jacoco会分析:指令(C0)、分支(C1)、行、方法、类型和循环复杂度的覆盖率

  1. 指令(Instructions,C0覆盖率,Java字节代码指令)
  2. 分支(Branches,C1覆盖率,分支覆盖率)
  3. 循环复杂度(Cyclomatic Complexity,cxty)
  4. 行(line)
  5. 方法(method)
  6. 类(class)

jacoco结果显示:

分支:

  • 无覆盖范围:该行没有分支执行(红色菱形)
  • 部分覆盖:仅执行了该行中的一部分分支(黄色菱形)
  • 全面覆盖:该行中的所有分支均已执行(绿色菱形)

行:

  • 无覆盖:该行中没有指令被执行(红色背景)
  • 部分覆盖:仅执行了该行中的一部分指令(黄色背景)
  • 全面覆盖:该行中的所有指令均已执行(绿色背景)

如果上面内容比较抽象,可以看看下面具体实例。

基本使用

maven配置

对于maven项目可以参考下面的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>vip.meet</groupId><artifactId>jacoco-test</artifactId><version>1.0.0</version><name>jacoco-test</name><description>单元测试报告</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.7.2</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF8</encoding></configuration><version>3.8.1</version></plugin><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.11</version><executions><execution><id>prepare-agent</id><goals><goal>prepare-agent</goal></goals><configuration><includes><!-- **/SayHi** --></includes><excludes></excludes></configuration></execution><execution><id>generate-code-coverage-report</id><phase>test</phase><goals><goal>report</goal></goals><configuration><formats>HTML</formats><includes>
<!--                                **/SayHi.class--></includes><excludes>
<!--                               **/SayHello.class--></excludes></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.22.2</version><configuration><skipTests>false</skipTests><testFailureIgnore>true</testFailureIgnore><forkMode>once</forkMode></configuration></plugin></plugins></build>
</project>

类与测试类

业务类:

public class SayHi {public static String hi(String name) {if (name == null) {return "名字不能为空";} else if (name.trim().isEmpty()) {return "名字不能为空字符串";} else {return "Hi " + name;}}
}
public class SayHello {public static String hello(String name) {if (name == null) {return "名字不能为空";} else if (name.trim().isEmpty()) {return "名字不能为空字符串";} else {return "Hello " + name;}}
}

测试类:

import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;class SayHiTest {@Testvoid getMessage() {assertEquals("Hi allen", SayHi.hi("allen"));SayHi.hi("");SayHi.hi(null);}
}
import org.junit.jupiter.api.Test;class SayHelloTest {@Testvoid hello() {SayHello.hello("allen");}
}

执行mvn命令,执行单元测试:

mvn clean test

输出的目录默认在项目目录下的target/site/jacoco目录下。

结果如下:

jacoco基本信息

其中1是指令覆盖率,2是分支覆盖率,后面是没有被覆盖到的复杂度(行、方法、类)与总复杂度(行、方法、类)

Total部分是汇总信息。

我们可以接着往下点,可以看到具体的类、方法、行的覆盖信息。

【具体方法行覆盖信息】
具体方法覆盖信息

jacoco常用配置

prepare-agent

jacoco.exec在这个阶段生成。

<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.11</version><executions><execution><id>prepare-agent</id><goals><goal>prepare-agent</goal></goals><configuration><classDumpDir>target/site/jacoco/med-class</classDumpDir><excludes><exclude>vip.meet.SayHello</exclude></excludes><includes><include>vip.meet.SayHi</include></includes></configuration></execution></executions>
</plugin>

report阶段

最常用的就是生成报告的时候,我们可能不想报告太乱,我们可以跳过某些类的,或者只为某些类生成报告。

<execution><id>generate-code-coverage-report</id><phase>test</phase><goals><goal>report</goal></goals><configuration><!--定义输出的文件夹--><outputDirectory>target/jacoco</outputDirectory><!--执行数据的文件--><dataFile>${project.build.directory}/jacoco.exec</dataFile><!--要从报告中排除的类文件列表,支持通配符(*和?)--><excludes>**/api/**/SayHello*.class</excludes><!--包含生成报告的文件列表,支持通配符(*和?)--><includes>**/SayHi.class</includes><!--HTML 报告页面中使用的页脚文本--><footer></footer><!--生成报告的文件类型,HTML(默认)、XML、CSV--><formats>HTML</formats><!--生成报告的编码格式,默认UTF-8--><outputEncoding>UTF-8</outputEncoding><!--跳过执行的标签--><skip></skip><!--源文件编码--><sourceEncoding>UTF-8</sourceEncoding><!--HTML报告的标题--><title>${project.name}-单元测试报告</title></configuration>
</execution>

例如,我只想看SayHi的覆盖测试情况,就可以在report阶段配置includes为 **/SayHi.class

rule-规则检查

<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.11</version><configuration><rules><rule implementation="org.jacoco.maven.RuleConfiguration"><element>BUNDLE</element><limits>  <!-- 方法覆盖率最小值为80% --><limit implementation="org.jacoco.report.check.Limit"><counter>METHOD</counter><value>COVEREDRATIO</value><minimum>0.8</minimum></limit><!-- 分支覆盖最小值为50% --><limit implementation="org.jacoco.report.check.Limit"><counter>BRANCH</counter><value>COVEREDRATIO</value><minimum>0.5</minimum></limit><!-- 类必须全部被覆盖 --><limit implementation="org.jacoco.report.check.Limit"><counter>CLASS</counter><value>MISSEDCOUNT</value><maximum>0</maximum></limit></limits></rule></rules>
</configuration>
</plugin>

rule参数:

  1. element:范围,bundle、package、class、sourcefile、method
  2. includes:需要检查的元素集合名
  3. excludes:不需要被检查的元素
  4. imits:用于检查的limits

limit参数:

  1. counter:INSTRUCTION(指令)、LINE(行)、BRANCH(分支)、COMPLEXITY(复杂度)、METHOD(方法)、CLASS(类)
  2. value:TOTALCOUNT(总数量)、MISSEDCOUNT(未覆盖数量)、COVEREDCOUNT(覆盖数量)、MISSEDRATIO(未覆盖率)、COVEREDRATIO(覆盖率)
  3. minimum:最小值
  4. maximum:最大值

rule检查不满足条件的时候,mvn test阶段直接报错。

rule是在check阶段执行的

 <execution><id>check</id><goals><goal>check</goal></goals>
</execution>

聚合项目配置

聚合项目

聚合项目可以单独添加一个子项目,来做聚合操作,只需配置:report-aggregate

<execution><id>jacoco-report-aggregate</id><phase>test</phase><goals><goal>report-aggregate</goal></goals>
</execution>

具体pom配置可以参考下面

父项目pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>vip.meet</groupId><artifactId>jacoco-aggregate-test</artifactId><version>1.0.0</version><packaging>pom</packaging><name>jacoco-test</name><description>单元测试覆盖率集合报告</description><modules><module>jacoco-one</module><module>jacoco-two</module><module>jacoco-aggregate</module></modules><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.7.2</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF8</encoding></configuration><version>3.8.1</version></plugin><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.11</version><executions><execution><id>prepare-agent</id><goals><goal>prepare-agent</goal></goals><configuration></configuration></execution><execution><id>generate-code-coverage-report</id><phase>test</phase><goals><goal>report</goal></goals><configuration><formats>HTML</formats></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.22.2</version><configuration><skipTests>false</skipTests><testFailureIgnore>true</testFailureIgnore><forkMode>once</forkMode></configuration></plugin></plugins></build>
</project>

配置聚合项目jacoco-maven-plugin其实最好放到子项目,按需配置,这里为了直观直接配置到了父项目的pom中,就意味着所有的子项目都继承了jacoco-maven-plugin。

我们可以看到配置了3个子项目:

<modules><module>jacoco-one</module><module>jacoco-two</module><module>jacoco-aggregate</module>
</modules>

jacoco-one和jacoco-two是正常的子项目,jacoco-aggregate是用来做聚合报告的。

主要需要看一下jacoco-aggregate的pom配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>vip.meet</groupId><artifactId>jacoco-aggregate-test</artifactId><version>1.0.0</version></parent><artifactId>jacoco-aggregate</artifactId><packaging>jar</packaging><dependencies><dependency><groupId>vip.meet</groupId><artifactId>jacoco-one</artifactId><version>1.0.0</version></dependency><dependency><groupId>vip.meet</groupId><artifactId>jacoco-two</artifactId><version>1.0.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.11</version><executions><execution><id>default-prepare-agent</id><goals><goal>prepare-agent</goal></goals><configuration></configuration></execution><execution><id>default-report</id><phase>test</phase><goals><goal>report</goal></goals></execution><execution><id>jacoco-report-aggregate</id><phase>test</phase><goals><goal>report-aggregate</goal></goals></execution></executions></plugin></plugins></build>
</project>

2点非常重要:

  1. 首先要通过dependency把需要统计覆盖率的项目依赖引入
  2. 单独配置jacoco-maven-plugin,因为需要report-aggregate

示例项目下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/564140.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis - list 列表

前言 列表类似于 Java 中的数组或者顺序表&#xff0c;在 Redis 中&#xff0c;可以对列表两端插⼊&#xff08;push&#xff09;和弹出&#xff08;pop&#xff09;&#xff0c;还可以获取指定范围的元素列表、 获取指定索引下标的元素等。列表是⼀种⽐较灵活的数据结构&#…

Loss-Attention

辅助信息 作者不开issue了&#xff0c;不建议复现

学习C++是否有必要学习Boost库?

C作为一门强大且灵活的编程语言&#xff0c;在软件开发领域有着广泛的应用。而在C的学习过程中&#xff0c;Boost库是一个经常被提及的重要资源。那么&#xff0c;对于C的学习者而言&#xff0c;是否有必要投入精力去学习Boost库呢&#xff1f;本文将就此问题展开详尽讨论。 一…

面试题:Java中的类加载器

1. 什么是类加载器&#xff0c;类加载器有哪些? 要想理解类加载器的话&#xff0c;务必要先清楚对于一个Java文件&#xff0c;它从编译到执行的整个过程。 类加载器&#xff1a;用于装载字节码文件(.class文件)运行时数据区&#xff1a;用于分配存储空间执行引擎&#xff1a;…

零代码编程:用kimichat批量重命名txt文本文件

一个文件夹中有很多个txt文本文件&#xff0c;需要全部进行重命名。 可以在kimichat中输入提示词&#xff1a; 你是一个Python编程专家&#xff0c;要完成一个关于批量重命名txt文本文件的Python脚本&#xff0c;下面是具体步骤&#xff1a; D:\Best Seller Books 这个文件夹…

chatGPT中文在线版本(亲测可用

ChatGPT是一个先进的自然语言处理模型&#xff0c;由OpenAI开发。它通过深度学习技术训练而成&#xff0c;可以进行对话、回答问题等多种自然语言处理任务。对于学生、开发者、研究人员和任何对人工智能感兴趣的人来说&#xff0c;这是一个非常有用的工具。 最近找到一个国内可…

【算法篇】逐步理解动态规划1(斐波那契数列模型)

目录 斐波那契数列模型 1. 第N个泰波那契数 2.使用最小花费爬楼梯 3.解码方法 学过算法的应该知道&#xff0c;动态规划一直都是一个非常难的模块&#xff0c;无论是状态转移方程的定义还是dp表的填表&#xff0c;都非常难找到思路。在这个算法的支线专题中我会结合很多力…

html网页制作-3D旋转卡片

网页制作-3D旋转卡片 两种旋转卡片的制作 旋转卡片&#xff08;1&#xff09; 代码 html代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-wid…

遍历目录下的某个文件并删除

目录 需求 编写过程 演示 需求 大家在学习时可能会有一个自己的小目录&#xff0c;里面放着各种奇葩代码&#xff0c;有天突然发现&#xff0c;没有空间了&#xff0c;这时候发现遗留了很多的可执行文件&#xff0c;大大的浪费了我们的空间&#xff0c;但是由于层数深&#…

汽车ABS的bangbang控制和模糊PID控制

1、内容简介 略 82-可以交流、咨询、答疑 2、内容说明 摘要&#xff1a;本文旨在设计一种利用模糊控制理论优化的pid控制器&#xff0c;控制abs系统&#xff0c;达到对滑移率最佳控制范围的要求 &#xff0c;所提出的方案采用级联控制架构&#xff1a;设计用于外环中的车轮打…

第四百二十二回

文章目录 1. 概念介绍2. 思路与方法2.1 实现思路2.2 实现方法 3. 示例代码4. 内容总结 我们在上一章回中介绍了"自定义标题栏"相关的内容&#xff0c;本章回中将介绍自定义Action菜单.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1. 概念介绍 我们在这里提到的…

基于大数据的空气质量预测和可视化分析

城市空气质量数据采集系统设计与实现 &#x1f3d9;️ 研究背景 &#x1f32c;️ 城市化与环境挑战&#xff1a;随着城市化进程的加快&#xff0c;环境污染问题&#xff0c;尤其是空气质量问题&#xff0c;已成为公众关注的焦点。数据监测的重要性&#xff1a;城市空气质量数…