junit5 动态测试

JUnit @TestFactory annotation coupled with DynamicTest can be used to create a test factory method.

可以将JUnit @TestFactory批注与DynamicTest结合使用,以创建测试工厂方法。

JUnit动态测试 (JUnit Dynamic Tests)

JUnit @TestFactory methods must not be private or static. These methods must return a Stream, Collection, Iterable, or Iterator of DynamicNode instances.

JUnit @TestFactory方法不得为私有或静态。 这些方法必须返回DynamicNode实例的Stream , Collection ,Iterable或Iterator 。

Any Stream returned by a @TestFactory will be properly closed by calling stream.close(), making it safe to use a resource such as Files.lines() as the initial source of the stream.

@TestFactory返回的任何Stream都可以通过调用stream.close()来正确关闭,从而可以安全地使用诸如Files.lines()类的资源作为流的初始源。

DynamicTest is one of the implementation of DynamicNode. Note that dynamic tests are different from @Test cases since callback methods such as @BeforeEach and @AfterEach are not executed for dynamic tests.

DynamicTestDynamicNode的实现之一。 请注意,动态测试与@Test情况不同,因为动态测试不会执行@BeforeEach和@AfterEach之类的回调方法。

JUnit @TestFactory DynamicTest示例 (JUnit @TestFactory DynamicTest Example)

Let’s look at a simple example of using @TestFactory and DynamicTest to create test factory of dynamic tests.

让我们看一个使用@TestFactory和DynamicTest创建动态测试的测试工厂的简单示例。

package com.journaldev.dynamictests;import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;import java.util.Arrays;
import java.util.Collection;import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;public class JUnit5DynamicTests {@TestFactoryCollection<DynamicTest> dynamicTests() {return Arrays.asList(dynamicTest("simple dynamic test", () -> assertTrue(true)),dynamicTest("My Executable Class", new MyExecutable()),dynamicTest("Exception Executable", () -> {throw new Exception("Exception Example");}),dynamicTest("simple dynamic test-2", () -> assertTrue(true)));}}class MyExecutable implements Executable {@Overridepublic void execute() throws Throwable {System.out.println("Hello World!");}}

Below image shows the output of the JUnit test execution.

下图显示了JUnit测试执行的输出。

Above example is very simple one, let’s create Dynamic tests for a custom class method. Let’s say we have a utility class defined as:

上面的例子很简单,让我们为自定义类方法创建动态测试。 假设我们有一个定义为的实用程序类:

public class MyUtils {public static int add(int x, int y) {return x+y;}
}

Here is the test factory method for above function.

这是上述功能的测试工厂方法。

@TestFactory
Stream<DynamicTest> dynamicTestsExample() {List<Integer> input1List = Arrays.asList(1,2,3);List<Integer> input2List = Arrays.asList(10,20,30);List<DynamicTest> dynamicTests = new ArrayList<>();for(int i=0; i < input1List.size(); i++) {int x = input1List.get(i);int y = input2List.get(i);DynamicTest dynamicTest = dynamicTest("Dynamic Test for MyUtils.add("+x+","+y+")", () ->{assertEquals(x+y,MyUtils.add(x,y));});dynamicTests.add(dynamicTest);}return dynamicTests.stream();
}

Below image shows the execution output of above test method.

下图显示了上述测试方法的执行输出。

Notice that our add method is simple, so in assertEquals() we are using input variables to derive the expected output. If it’s a complex method, we can define a List for expected output and use that in the assertions. We can also define a custom Executable class if we want to have complex testing logic.

请注意,我们的add方法很简单,因此在assertEquals()我们使用输入变量来得出预期的输出。 如果这是一个复杂的方法,我们可以为预期的输出定义一个List并将其用于断言中。 如果我们想要复杂的测试逻辑,我们还可以定义一个自定义的Executable类。

摘要 (Summary)

JUnit 5 Dynamic tests functionality can be achieved by parameterized tests. Also, Parameterized tests follow the standard JUnit test lifecycle and @BeforeEach and @AfterEach methods are executed for them. Whereas dynamic tests lifecycle is totally different and they don’t have access to @BeforeEach and @AfterEach methods.

JUnit 5动态测试功能可以通过参数化测试来实现。 同样,参数化测试遵循标准的JUnit测试生命周期,并对其执行@BeforeEach和@AfterEach方法。 动态测试的生命周期完全不同,并且无法访问@BeforeEach和@AfterEach方法。

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

翻译自: https://www.journaldev.com/21715/junit-dynamic-tests-testfactory-dynamictest

junit5 动态测试

junit5 动态测试_JUnit 5动态测试– @ TestFactory,DynamicTest相关推荐

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

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

  2. junit5 动态测试_JUnit 5嵌套测试

    junit5 动态测试 JUnit Jupiter @Nested annotation can be used to mark a nested class to be included in th ...

  3. JUnit 5 –动态测试

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

  4. Java单元测试框架与实践(Junit5 + Mockito)

    Java单元测试框架与实践 本文首先在理论上归纳了单元测试在宏观和微观层面要遵循的基本原则,以及测试覆盖率的要求和评价维度.然后具体阐述了笔者实战中总结的基于Junit + Mockito 的单元测试 ...

  5. junit5 入门系列教程-02-junit5 注解详解

    目录 目录 Junit5 注解 @Test @ParameterizedTest @RepeatedTest @TestFactory @TestInstance @TestTemplate @Dis ...

  6. JUnit 5 简介

    转自:https://www.ibm.com/developerworks/cn/java/j-introducing-junit5-part1-jupiter-api/index.html http ...

  7. 软件测试面试总结——常见的面试问题

    前言 在这里还分享一波我在B站看到面试相关的视频吧!感兴趣的小伙伴可以去看看 https://www.bilibili.com/video/BV1p44y1H7Nc [2022最新软测面试合集]备战金 ...

  8. 网上读书关于软件测试,【读书笔记】之软件测试

    1.引论 1.1 什么是软件测试 软件测试主要是对制作的软件产品进行检查和测试,及时地发现程序中的故障和逻辑错误,以保障软件产品的可靠性.软件测试是保证软件质量的关键步骤,也是提高软件可靠性的重要手段 ...

  9. 基于Java的飞机大战游戏的设计与实现论文

    源码下载 http://www.byamd.xyz/hui-zong-1/ 摘 要 现如今,随着智能手机的兴起与普及,加上4G(the 4th Generation mobile communicat ...

最新文章

  1. Windows Server 2012 HyperV之SMB共享实时迁移
  2. 简单的在jsp页面操作mysql
  3. 一步一步学ROP之gadgets和2free篇
  4. LInux查看CPU状态
  5. 童话镇计算机乐谱,童话镇简谱(歌词)-陈一发演唱-桃李醉春风记谱
  6. 爬虫——正则表达式re模块
  7. JavaFX 2.1:Toolkit not initialized
  8. Hibernate 补充 ManyToOne、OneToMany、OneToOne的使用例
  9. c++101rule
  10. android x86 uc,android x86 固件定制
  11. 老板为什么越来越难熬?
  12. git删除本地tag和远程tag
  13. 2020年内蒙古自治区第十五届大学生程序设计竞赛榜单
  14. java 世界时间_Java对世界不同时区timezone之间时间转换的处理方法
  15. 虚拟机导致无法上网_虚拟机无法上网问题解决方法
  16. firefox插件开发和调试
  17. 煲耳机,看到一篇文章分享下
  18. 最后冲刺—信息系统开发与管理
  19. 3月16日----3月20日二年级课程表
  20. 修改Excel时出现“被保护单元格不支持此功能“的解决办法

热门文章

  1. 清空所有textbox
  2. [转载] python win32api 使用小技巧
  3. 《Linux命令行与shell脚本编程大全 第3版》
  4. oracle 查看最大连接数与当前连接数
  5. Gym 101246G Revolutionary Roads
  6. iOS应用日志:开始编写日志组件与异常日志
  7. MySQL 5.6 dump/load buffer pool实验
  8. js Function.call
  9. SQLServer如何取得随机获取的数据库记录
  10. (转)什么时候加上android.intent.category.DEFAULT和LAUNCHER