junit5 动态测试

JUnit Jupiter @Nested annotation can be used to mark a nested class to be included in the test cases. When JUnit tests are executed, Nested classes are not scanned for test methods. We can explicitly mark them to be scanned for test cases using @Nested annotation.

JUnit Jupiter @Nested批注可用于标记要包含在测试用例中的嵌套类。 执行JUnit测试时,不会扫描嵌套类的测试方法。 我们可以使用@Nested批注明确标记要扫描的测试用例。

JUnit 5嵌套测试 (JUnit 5 Nested Tests)

JUnit Nested test classes should be non-static. Let’s look at a simple example of JUnit 5 Nested test class.

JUnit嵌套测试类应该是非静态的。 让我们看一个简单的JUnit 5嵌套测试类示例。

package com.journaldev.nested;import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;public class NestedTestSimpleExample {@Testvoid test() {System.out.println("Outer Class Test");}@Nestedclass InnerClass {@Testvoid test() {System.out.println("Inner Class Test");}}
}

Below image shows the JUnit execution report from Eclipse.

下图显示了Eclipse的JUnit执行报告。

If we remove @Nested annotation from the inner class, then the tests inside that will not be executed.

如果我们从内部类中删除@Nested批注,则其中的测试将不会执行。

JUnit嵌套测试回调方法 (JUnit Nested Tests Callback Methods)

Nested test classes can have their own @BeforeEach and @AfterEach methods. However, if outer class @BeforeEach and @AfterEach methods will also get executed for nested class tests. Let’s extend our example to confirm this behavior.

嵌套测试类可以具有自己的@BeforeEach@AfterEach方法。 但是,如果外部类@BeforeEach@AfterEach方法也将为嵌套类测试执行。 让我们扩展示例以确认此行为。

package com.journaldev.nested;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;public class NestedTestSimpleExample {@BeforeAllstatic void setUpBeforeClass() throws Exception {System.out.println("@BeforeAll - Outer Class");}@AfterAllstatic void tearDownAfterClass() throws Exception {System.out.println("@AfterAll - Outer Class");}@BeforeEachvoid setUp() throws Exception {System.out.println("@BeforeEach - Outer Class");}@AfterEachvoid tearDown() throws Exception {System.out.println("@AfterEach - Outer Class");}@Testvoid test() {System.out.println("Outer Class Test");}@Nestedclass InnerClass {@BeforeEachvoid setUp() throws Exception {System.out.println("@BeforeEach - Inner Class");}@AfterEachvoid tearDown() throws Exception {System.out.println("@AfterEach - Inner Class");}@Testvoid test() {System.out.println("Inner Class Test");}}
}

The console output of JUnit Test Execution clearly confirms this behavior.

JUnit测试执行的控制台输出清楚地确认了此行为。

@BeforeAll - Outer Class@BeforeEach - Outer Class
Outer Class Test
@AfterEach - Outer Class@BeforeEach - Outer Class
@BeforeEach - Inner Class
Inner Class Test
@AfterEach - Inner Class
@AfterEach - Outer Class@AfterAll - Outer Class

If we want @BeforeAll and @AfterAll methods in the nested test class, then we have to explicitly set its lifecycle behavior to “per-class”. The default behavior of JUnit test cases is “per-method”.

如果要在嵌套测试类中使用@BeforeAll和@AfterAll方法,则必须将其生命周期行为显式设置为“每个类”。 JUnit测试用例的默认行为是“按方法”。

package com.journaldev.nested;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;class NestedExampleTest {@BeforeAllstatic void setUpBeforeClass() throws Exception {System.out.println("@BeforeAll - Outer Class");}@AfterAllstatic void tearDownAfterClass() throws Exception {System.out.println("@AfterAll - Outer Class");}@BeforeEachvoid setUp() throws Exception {System.out.println("@BeforeEach - Outer Class");}@AfterEachvoid tearDown() throws Exception {System.out.println("@AfterEach - Outer Class");}@Testvoid outer_test() {System.out.println("Outer Class test method");}@Nested@TestInstance(Lifecycle.PER_CLASS)class InnerClass {@BeforeAllvoid setUpBeforeClassInner() throws Exception {System.out.println("@BeforeAll - Inner Class");}@AfterAllvoid tearDownAfterClassInner() throws Exception {System.out.println("@AfterAll - Inner Class");}@BeforeEachvoid setUp() throws Exception {System.out.println("@BeforeEach - Inner Class");}@AfterEachvoid tearDown() throws Exception {System.out.println("@AfterEach - Inner Class");}@Testvoid inner_test() {System.out.println("Inner Class test method");}}
}

Here is the console output of JUnit test:

这是JUnit测试的控制台输出:

@BeforeAll - Outer Class@BeforeEach - Outer Class
Outer Class test method
@AfterEach - Outer Class@BeforeAll - Inner Class@BeforeEach - Outer Class
@BeforeEach - Inner Class
Inner Class test method
@AfterEach - Inner Class
@AfterEach - Outer Class@AfterAll - Inner Class
@AfterAll - Outer Class

摘要 (Summary)

JUnit Nested Classes can be used to categorize our test cases into different modules. But the same thing can be achieved by having multiple test classes.

JUnit嵌套类可用于将我们的测试用例分类到不同的模块中。 但是,通过具有多个测试类可以实现相同的目的。

GitHub Repository.GitHub Repository中检出完整的代码。

翻译自: https://www.journaldev.com/21731/junit-nested-tests

junit5 动态测试

junit5 动态测试_JUnit 5嵌套测试相关推荐

  1. junit5 动态测试_JUnit 5 –动态测试

    junit5 动态测试 在定义测试时,JUnit 4有一个很大的弱点:它必须在编译时发生. 现在,JUnit 5将解决此问题! Milestone 1 刚刚发布 ,它带有全新的动态测试,可以在运行时创 ...

  2. junit5 动态测试_JUnit 5动态测试– @ TestFactory,DynamicTest

    junit5 动态测试 JUnit @TestFactory annotation coupled with DynamicTest can be used to create a test fact ...

  3. Springboot-21单元测试(Junit5、assertions、assumptions、嵌套测试、参数化测试)

    1.JUnit5 的变化 Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库 作为最新版本的JUnit框架由,三个不同子项目的几个不同模块组成. JUnit 5 = ...

  4. 七、springboot 单元测试阶段 (4、前置条件(assumptions)5、嵌套测试 6、参数化测试 7、迁移指南)

    目录 4.前置条件(assumptions) 5.嵌套测试 6.参数化测试 7.迁移指南 4.前置条件(assumptions) JUnit 5 中的前置条件(assumptions[假设])类似于断 ...

  5. 75、单元测试-嵌套测试

    官方文档 - Nested Tests JUnit 5 可以通过 Java 中的内部类和@Nested 注解实现嵌套测试,从而可以更好的把相关的测试方法组织在一起.在内部类中可以使用@BeforeEa ...

  6. JUnit5学习之七:参数化测试(Parameterized Tests)进阶

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于<JUnit5学习>系列 <JU ...

  7. junit白盒测试 案例_JUnit通过失败测试案例

    junit白盒测试 案例 为什么要建立一种预期测试失败的机制? 有一段时间,人们会希望并期望JUnit @Test案例失败. 尽管这种情况很少见,但确实发生了. 我需要检测JUnit测试何时失败,然后 ...

  8. junit不启用事务_JUnit禁用启用测试

    junit不启用事务 JUnit 5 Jupiter API provides various ways to disable or enable a test. There are various ...

  9. JUnit5学习之六:参数化测试(Parameterized Tests)基础

    | :-- | :-- | :-- | | 项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 | | git仓库地址(https ...

最新文章

  1. 安装 Enthought Tool Suite 时遇到的问题
  2. php wordpress 开源,PHP 遭弃用!WordPress.com 开源并转用 Javascript
  3. pandas 指定某一列的数据类型
  4. MFC中STL容器中Vector,List,Map基本用法汇总
  5. 驱动之LCD的介绍与应用20170209
  6. 载波聚合或双连接的方式进行_处理载波聚合及双连接的装置及方法与流程
  7. 学习思考 耐得寂寞 拥得繁华
  8. 怎样写出别人无法维护的代码
  9. HTML5 Canvas的基本用法
  10. Android开发文档
  11. php5.6 mongo 扩展,docker php5.6镜像创建,包括常用扩展安装
  12. MediaCodec解码aac
  13. 为什么产品经理总被吐槽是”水货“
  14. VMware中安装Win10超详细步骤
  15. windows批处理学习
  16. 单片机ESD静电防护总结
  17. java color 棕色,没想到红棕色也有失宠的一天?
  18. Windows程序设计-09-COM技术
  19. 什么是单点登录?单点登录的解决方案
  20. 《Unity开发实战》——1.3节设置首选项

热门文章

  1. JQuery ajax 在aspx中传值和取值
  2. ORA-00923: 未找到要求的 FROM 关键字
  3. C++ 内存分配 学习笔记
  4. C#泛型学习实例(简单易懂)
  5. FreeBSD下nginx并支持php配置详解
  6. [转载] 说说Python 面向对象编程
  7. FPGA开发设计必经之路:时序分析
  8. install memcached for ubuntu
  9. Linux编程获取本机IP地址
  10. 1135(重、错)Is It A Red-Black Tree