Tag

JUnit5可以使用@Tag注解给测试类和测试方法打tag,这些tag能用来在执行时进行过滤,它跟group有点类似。

tag应该遵循以下规则:

  • 不能为null或者为空。
  • 不能包含空格。
  • 不能包含ISO控制字符。
  • 不能包含保留字符:, ( ) & | !

示例代码:

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; @Tag("fast")
@Tag("model")
class TaggingDemo { @Test
@Tag("taxes")
void testingTaxCalculation() {
} }

@Tag还可以通过元注解和组合注解,实现自定义注解,参考:

https://dongfanger.gitee.io/blog/JUnit/002-JUnit5注解学习指引.html#id2

Filter

打好了tag后,在执行时可以进行过滤,比如Maven配置:

<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<groups>acceptance | !feature-a</groups>
<excludedGroups>integration, regression</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
<!-- ... -->

groups用来指定包含哪些tag,excludedGroups用来指定排除哪些tag。

Order

JUnit5默认使用了某种算法来确定test方法的执行顺序。我们可以通过@TestMethodOrder进行自定义,既可以使用内置类,也可以使用实现了MethodOrderer接口的类。

内置类如下:

  • DisplayName 按DisplayName的字母数字顺序
  • OrderAnnotation 通过@Order注解指定顺序
  • Random 随机顺序
  • Alphanumeric 按test方法名和参数列表的字母数字顺序

示例:

import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; @TestMethodOrder(OrderAnnotation.class)
class OrderedTestsDemo { @Test
@Order(1)
void nullValues() {
// perform assertions against null values
} @Test
@Order(2)
void emptyValues() {
// perform assertions against empty values
} @Test
@Order(3)
void validValues() {
// perform assertions against valid values
} }

也可以配置全局的JUnit5的默认执行顺序,比如在src/test/resources/junit-platform.properties中:

junit.jupiter.testmethod.order.default = \
org.junit.jupiter.api.MethodOrderer$OrderAnnotation

Lifecycle

JUnit5默认会在执行测试方法前给每个测试类创建一个实例对象,让测试方法相互独立,这叫做per-method测试实例生命周期。

就算测试方法被disable了也会创建实例。

如果想让每个测试类只创建一个实例对象,测试方法共用这一个实例,那么可以使用注解@TestInstance(Lifecycle.PER_CLASS),这叫做pre-class测试实例生命周期。

pre-class有一些好处,比如:

  • 在实例中存储变量,然后通过@BeforeEach@AfterEach修改。
  • @BeforeAll@AfterAll可以作用于非静态方法和接口default方法。
  • @BeforeAll@AfterAll可以作用于@Nested嵌套测试类。

有两种方式可以设置全局的生命周期模式,第一种是JVM启动参数:

-Djunit.jupiter.testinstance.lifecycle.default=per_class

第二种方式是配置文件,比如src/test/resources/junit-platform.properties

junit.jupiter.testinstance.lifecycle.default = per_class

如果要进行全局配置,建议使用配置文件,这样在出现问题时方便进行追溯。

小结

本文首先介绍了给测试类和测试方法打tag进行分组,然后可以在运行时根据tag进行过滤,接着介绍了如何制定测试方法的执行顺序,最后介绍了两种生命周期:per-methodpre-class

参考资料:

https://junit.org/junit5/docs/current/user-guide/#writing-tests-tagging-and-filtering

https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order

https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle

JUnit5的Tag、Filter、Order、Lifecycle的更多相关文章

  1. html tag filter in js

    html tag filter in js const html = `可当天预订,必须21时15分之前下单,要求必须<font color=green><b>60</b ...

  2. How to define Servlet filter order of execution using annotations

    If we define Servlet filters in web.xml, then the order of execution of the filters will be the same ...

  3. Filter execute order in asp.net web api

    https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api Som ...

  4. Sallen-Key Active Butterworth Low Pass Filter Calculator

    RC 2nd Order Passive Low Pass Filter The cut-off frequency of second order low pass filter is given ...

  5. Spring boot下添加filter

    https://www.cnblogs.com/OnlyCT/p/7133639.html ****************************************************** ...

  6. SpringBoot(四)-- 整合Servlet、Filter、Listener

    SpringBoot中有两种方式可以添加 Servlet.Filter.Listener. 1.代码注册 通过ServletRegistrationBean. FilterRegistrationBe ...

  7. 简说MVC Filter

    Filter与FilterProvider之间的关系 根据用途和执行时机的不同,MVC主要分为以下5种类型的过虑器:AuthenticationFilter.AuthorizationFilter.A ...

  8. Spring boot 学习八 Springboot的filter

    一:  传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...

  9. springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径

    springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径,没有配置/*,输入任何路径都能进过滤器 2019年04月25日 12:51:33 peigui.hu ...

随机推荐

  1. Linux巡检常用命令

    # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...

  2. Spring IoC容器 XML 配置与加载

    IoC 容器 XML 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  3. Nginx下配置Https 配置文件(vue)

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  4. 深入剖析 MySQL 自增锁

    之前的文章把 InnoDB 中的所有的锁都介绍了一下,包括意向锁.记录锁...自增锁巴拉巴拉的.但是后面我自己回过头去看的时候发现,对自增锁的介绍居然才短短的一段. 其实自增锁(AUTO-INC Lo ...

  5. Nginx 配置实例-配置动静分离

    Nginx 配置实例-配置动静分离 1. 静态资源的创建 2. nginx 动静分离的配置 3. 验证 1. 静态资源的创建 这里使用的静态资源主要为 HTML 静态文件和图片. mkdir -vp ...

  6. Git如何下载clone指定的tag

    Git如何下载clone指定的tag 如上图,我想下载Tags标签为solution-4 的代码,如何处理呢? 命令如下: git clone --branch solution-4 git@gith ...

  7. H.265视频编码与技术全析(下)

    H.265视频编码与技术全析(下) 四.帧内预测模式 共35个(h264有9个),包括Planar,DC,33个方向模式: 除了Intra_Angular预测外,HEVC还和H.264/MPEG-4 ...

  8. 【NX二次开发】Block UI 角度尺寸

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  9. 【NX二次开发】获取相邻面UF_MODL_ask_adjac_faces

    获取箭头指示的面的相邻面 源码: 1 extern DllExport void ufsta(char *param, int *returnCode, int rlen) 2 { 3 UF_init ...

  10. 【NX二次开发】uf5945获得旋转矩阵、uf5947根据变换矩阵移动或复制对象

    返回一个矩阵,可以绕任意轴旋转. 与uf5947结合可以将对象沿着任意轴进行旋转.不是所有对象都能用uf5947变换,带参的实体.部件都不可以用此函数变换.下面是旋转WCS的例子. extern Dl ...