软件测试丨单元测试框架怎么搭?新版的 Junit5 有哪些神奇之处?
单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。
总的来说,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程中要进行的最低级别的测试活动,软件的独立单元将在与程序的其他部分相隔离的情况下进行测试。
那单元测试框架该怎么搭呢?Junit5 又能给我们带来怎样的惊喜呢?首先我们来看看什么是 Junit5,再看看如何使用吧~
What is Junit5?
Junit5 简介
什么是 Junit5,在 Junit5 的官方介绍文档中这写到:Junit5 由JUnit Platform
+ JUnit Jupiter
+ JUnit Vintage3
部分构成
借用 IBM Developer 的一张图来说明 JUnit 5 的架构:

JUnit Platform :
其主要作用是在 JVM 上启动测试框架。它定义了一个抽象的 TestEngine API 来定义运行在平台上的测试框架;也就是说其他的自动化测试引擎或开发人员⾃⼰定制的引擎都可以接入 Junit 实现对接和执行。同时还支持通过命令行、Gradle 和 Maven 来运行平台(这对于我们做自动化测试至关重要)
JUnit Jupiter:
这是 Junit5 的核心,可以看作是承载 Junit4 原有功能的演进,包含了 JUnit 5 最新的编程模型和扩展机制;很多丰富的新特性使 JUnit ⾃动化测试更加方便、功能更加丰富和强大。也是测试需要重点学习的地方;Jupiter 本身也是⼀一个基于 Junit Platform 的引擎实现,对 JUnit 5 而言,JUnit Jupiter API 只是另一个 API!
JUnit Vintage:
Junit 发展了 10 数年,Junit 3 和 Junit 4 都积累了大量的⽤用户,作为新一代框架,这个模块是对 JUnit3,JUnit4 版本兼容的测试引擎,使旧版本 junit 的⾃动化测试脚本也可以顺畅运行在 Junit5 下,它也可以看作是基于 Junit Platform 实现的引擎范例。
JUnit 5 对 Java 运行环境的最低要求是 Java 8。
Junit5 的新特性
特性与 JUnit4 注解比较
JUnit5 的 新特性有:嵌套单元测试、Lambda 支持、参数化测试、重复测试、动态测试
JUnit 4 与 JUnit 5 中的注解比较

JUnit 5 常用注解
Junit5 常用注解展示

实操演示
1)创建 maven 工程 XUnit,pom.xml
中添加Junit5
的依赖
2)其余的 XUnit 框架通用的设计运行规则可参考博客:如何利用 xUnit 框架对测试用例进行维护-xUnit 简介及基本使用方法(基于 Junit4)
3)添加用例@Test
,再在用例执行前后添加@BeforeEach
、@AfterEach
:

运行结果:

4)在测试类执行前后添加@BeforeAll
和@AfterAll

测试结果:

5)在测试用例test1
上加入注解@Disabled
,使test1
失

测试结果:

从测试结果中我们可以看到 test1 用例被 ignore,没有被执行
6)分别将 test1 和 test2 用@DisplayName
加上用例展示名称

测试结果:

7)对测试用例 2 加上注解@RepeatedTest
,使其额外重复执行 3 次

测试结果:

从测试结果中我们可以看到测试用例 2 被额外重复执行了 3 次
8)对于@Nested
嵌套执行举例如下:

测试结果:

由测试结果可以看出,@Nested
的执行顺序为先执行@Nested
嵌套外层的用例,再以倒叙形式执行@Nested
用例,然后再执行第二层嵌套的用例:
外层->倒叙嵌套->第二层嵌套
Junit5 套件执行
套件介绍

@RunWith 是从 Junit4 迁移过来的,@RunWith 连同它的参数 JUnitPlatform.class(一个基于 JUnit 4 且理解 JUnit Platform 的 Runner)让您可以在 Eclipse 内运行 JUnit Jupiter 单元测试。Eclipse 尚未原生支持 JUnit 5。未来,Eclipse 将提供原生的 JUnit 5 支持,那时我们不再需要此注解;Junit5 官方给出了替代它的注解:
@RunWith+@SelectPackages 有两个包 testcasedemo, junit5demo,利用 @RunWith+@SelectPackages 将包中测试类依次运行

套件类:

测试结果:

@RunWith+@SelectPackages+@IncludePackage
@RunWith
+@SelectPackages
+@IncludePackages
配合使用过滤出需要执行的测试包testcasedemo.demo2

套件类:

测试结果:

@RunWith+@SelectPackages+@ExcludePackages
@RunWith
+@SelectPackages
+@ExcludePackages
配合使用过滤出不需要执行的测试包testcasedemo.demo2
套件类:

测试结果:

@RunWith+@SelectPackages+@IncludeClassNamePatterns
将 junit5demo 包下的 TestJunit5demo 和 testcasedemo.demo2 所有测试类过滤出来并执行
套件类:

测试结果

@RunWith+@SelectPackages+@IncludeTags 在testcasedemo.demo2.TestDemo2
的方法testDemo2
上加上注解@Tag
:

过滤并执行方法 testDemo2:
套件类:

测试结果:

参考文档
Junit5 官网:
https://junit.org/junit5/docs/current/user-guide/#overview
IBM Developer:
https://www.ibm.com/developerworks/cn/java/j-introducing-junit5-part1-jupiter-api/index.html
https://www.ibm.com/developerworks/cn/java/j-junit5/index.html
版权声明: 本文为 InfoQ 作者【测试人】的原创文章。
原文链接:【http://xie.infoq.cn/article/28008f654b64a8cc1bda81202】。文章转载请联系作者。
评论