JUnit5的Tag、Filter、Order、Lifecycle
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-method和pre-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的更多相关文章
- html tag filter in js
html tag filter in js const html = `可当天预订,必须21时15分之前下单,要求必须<font color=green><b>60</b ...
- 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 ...
- Filter execute order in asp.net web api
https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api Som ...
- 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 ...
- Spring boot下添加filter
https://www.cnblogs.com/OnlyCT/p/7133639.html ****************************************************** ...
- SpringBoot(四)-- 整合Servlet、Filter、Listener
SpringBoot中有两种方式可以添加 Servlet.Filter.Listener. 1.代码注册 通过ServletRegistrationBean. FilterRegistrationBe ...
- 简说MVC Filter
Filter与FilterProvider之间的关系 根据用途和执行时机的不同,MVC主要分为以下5种类型的过虑器:AuthenticationFilter.AuthorizationFilter.A ...
- Spring boot 学习八 Springboot的filter
一: 传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...
- springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径
springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径,没有配置/*,输入任何路径都能进过滤器 2019年04月25日 12:51:33 peigui.hu ...
随机推荐
- LNMP/LAMP
LNMP/LAMP 环境: 名称 Linux Nginx MySQL PHP Apache 版本 Centos7 nginx-1.14.1 mysql-5.6.25 php-5.6.36 Apache ...
- 重定向-管道技术-xargs命令详解
重定向 什么是重定向? 将原本要输出在屏幕的内容,重新定向输出到指定的文件或设备中. 为什么要使用重定向? 1.备份的时候需要知道备份的结果. 2.屏幕上输出信息比较重要的时候需要保存下来. 3.定时 ...
- Log4j 配置多个 Logger
引言 Log4j 是 Java 的主流日志框架,通过灵活的配置可以提供各种类型的日志服务. 在使用 Log4j 进行实际项目开发的过程中,有时不想使用 rootLogger 记录器,把所有的日志都输出 ...
- S6 文件备份与压缩命令
6.1 tar:打包备份 6.2 gzip:压缩或解压文件 6.3-4 zip.unzip 6.5 scp:远程文件复制 6.6 rsync:文件同步工具
- 在vue中使用prismjs
wqy的笔记:http://www.upwqy.com/details/261.html 作者:wqy 1 首先在项目中安装prismjs插件: cnpm install prismjs -S 2 安 ...
- Windows 10 版本 21H1 发布,百度网盘下载
请访问原文链接:https://sysin.org/article/windows-10/,查看最新版.原创作品,转载请保留出处. Windows 10, version 21H1, all edit ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- Python-Redis-常用操作&管道
常用操作 1.1 delete(*names) ? 1 2 3 4 5 6 7 8 9 # 根据删除redis中的任意数据类型 print(r.get('name')) r.delete('nam ...
- CVPR2020行人重识别算法论文解读
CVPR2020行人重识别算法论文解读 Cross-modalityPersonre-identificationwithShared-SpecificFeatureTransfer 具有特定共享特征变换 ...
- 全景分割:CVPR2019论文解析
全景分割:CVPR2019论文解析 Panoptic Segmentation 论文链接: http://openaccess.thecvf.com/content_CVPR_2019/papers/ ...