通过 JUnit5 @RepeatedTest注解,可以编写可以多次运行的可重复测试模板。 频率可以配置为@RepeatedTest注解的参数。

1. @RepeatedTest注解用法

要创建可重复的测试模板方法,请使用@RepeatedTest注解该方法。

@DisplayName("Add operation test")
@RepeatedTest(5)
void addNumber(TestInfo testInfo) {Calculator calculator = new Calculator();Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
}

在上面的代码中,addNumber()测试将重复 5 次。

请注意,每次重复测试的行为都类似于常规@Test方法的执行,并且完全支持相同的生命周期回调和扩展。 这意味着对于每个单独的调用,将在适合它们的测试生命周期的地方调用@BeforeEach和@AfterEach带注解的方法。

package com.howtodoinjava.junit5.examples;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;@RunWith(JUnitPlatform.class)
public class RepeatedTestExample {@BeforeAllpublic static void init(){System.out.println("Before All init() method called");}@BeforeEachpublic void initEach(){System.out.println("Before Each initEach() method called");}@DisplayName("Add operation test")@RepeatedTest(5)void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) {System.out.println("Running addNumber test -> " + repetitionInfo.getCurrentRepetition());Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");}@AfterEachpublic void cleanUpEach(){System.out.println("After Each cleanUpEach() method called");}@AfterAllpublic static void cleanUp(){System.out.println("After All cleanUp() method called");}
}

上面程序的输出:

Before All init() method calledBefore Each initEach() method called
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 1
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 2
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 3
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 4
After Each cleanUpEach() method calledBefore Each initEach() method called
Running addNumber test -> 5
After Each cleanUpEach() method calledAfter All cleanUp() method called

2. 自定义显示测试名称

除了指定重复次数之外,您还可以为每个重复指定自定义显示名称。此自定义显示名称可以是静态文本+动态占位符的组合。 当前,支持 3 个占位符:

  • {displayName}:@RepeatedTest方法的显示名称。
  • {currentRepetition}:当前重复计数。
  • {totalRepetitions}:重复总数。
@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample
{@DisplayName("Add operation test")@RepeatedTest(value = 5, name = "{displayName} - repetition {currentRepetition} of {totalRepetitions}")void addNumber(TestInfo testInfo) {Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");}
}

运行以上测试将在下面输出:


JUnit5 重复测试的显示名称

您可以使用两种预定义格式之一,即RepeatedTest.LONG_DISPLAY_NAMERepeatedTest.SHORT_DISPLAY_NAME。 如果未指定,则SHORT_DISPLAY_NAME是默认格式。

  • RepeatedTest.LONG_DISPLAY_NAME – {displayName} :: repetition {currentRepetition} of {totalRepetitions}
  • RepeatedTest.SHORT_DISPLAY_NAME – repetition {currentRepetition} of {totalRepetitions}
@DisplayName("Add operation test")
@RepeatedTest(value = 5, name = RepeatedTest.LONG_DISPLAY_NAME)
void addNumber(TestInfo testInfo) {Assertions.assertEquals(2, Calculator .add(1, 1), "1 + 1 should equal 2");
}

3. RepetitionInfo接口

RepetitionInfo用于将有关重复测试的当前重复的信息注入@RepeatedTest,@BeforeEach和@AfterEach方法中。

@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample {@BeforeEachpublic void initEach(RepetitionInfo info){int currentRepetition = info.getCurrentRepetition();int totalRepetitions = info.getTotalRepetitions();//Use information as needed}@DisplayName("Add operation test")@RepeatedTest(value = 5, name="{displayName} :: repetition {currentRepetition} of {totalRepetitions}")void addNumber(TestInfo testInfo) {Calculator calculator = new Calculator();Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");}@AfterEachpublic void cleanUpEach(RepetitionInfo info){int currentRepetition = info.getCurrentRepetition();int totalRepetitions = info.getTotalRepetitions();//Use information as needed}
}

JUnit5 @RepeatedTest注解示例相关推荐

  1. JUnit5 @AfterAll注解示例

    JUnit5 @AfterAll 注解替换了 JUnit4 中的@AfterClass注解.它用于表示应在当前测试类中的所有测试之后执行注解方法. @AfterAll注解用法 使用@AfterAll注 ...

  2. JUnit5 @AfterEach注解示例

    JUnit5 @AfterEach 注解替换了 JUnit4 中的@After注解.它用于表示应在当前类中的每个@Test方法之后执行注解方法. @AfterEach注解用法 使用@AfterEach ...

  3. JUnit5 @BeforeEach注解示例

    JUnit5 @BeforeEach注解替换了 JUnit4 中的@Before注解.它用于表示应在当前类中的每个@Test方法之前执行注解方法. @BeforeEach注解用法 使用@BeforeE ...

  4. JUnit5 @BeforeAll注解示例

    JUnit5 @BeforeAll 注解替换了 JUnit4 中的@BeforeClass注解. 它用于表示应在当前测试类中的所有测试之前执行注解方法. @BeforeAll注解用法 使用@Befor ...

  5. JUnit5 @Tag注解示例

    JUnit5 @Tag 可用于从测试计划中过滤测试用例. 它可以帮助针对不同的环境,不同的用例或任何特定要求创建多个不同的测试计划.您可以通过仅在测试计划中包括那些标记的测试或通过从测试计划中排除其他 ...

  6. 七、springboot 单元测试阶段 (2、JUnit5常用注解,3、断言(assertions) )

    2.JUnit5常用注解 JUnit5的注解与JUnit4的注解有所变化 JUnit 5 User Guide @Test :表示方法是测试方法.但是与JUnit4的@Test不同,他的职责非常单一不 ...

  7. JUnit5 测试套件示例

    使用 JUnit5 测试套件,您可以运行分散到多个测试类和不同包中的测试. JUnit5 提供了两个注解:@SelectPackages和@SelectClasses以创建测试套件. 此外,您可以使用 ...

  8. Spring @RequestMapping注解示例

    在 spring mvc hello world 应用程序中,我们看到了具有端到端功能(不包括任何数据库访问)的非常基本的员工管理应用程序. 在学习spring mvc 模块的下一步中,我将提供@Re ...

  9. JUnit5 @Disabled测试示例

    JUnit @Disabled注解可用于禁用测试套件中的测试方法. 该注解可以应用于测试类以及各个测试方法. 它仅接受一个可选参数,它指示此测试被禁用的原因. @Disabled的测试类 将@Disa ...

最新文章

  1. 搭建基于虚拟账号的电子邮件系统
  2. leetcode数组汇总_LeetCode刷题实战43:字符串相乘
  3. Normalizing Flows Tutorial(标准化流教程)第一部分
  4. sublime java 编译_03 sublime text3下配置Java的编译运行环境
  5. python3使用smtplib通过qq邮箱发送邮件
  6. paip.重装系统需要备份的资料总结..v2.0 cad
  7. Photoshop入门教程十个点
  8. goland设置goroot_goland安装
  9. zepto 自定义打包
  10. python怎么画蝙蝠_蝙蝠算法学习
  11. EBSD测试常见的问题及解答(二)
  12. 洛谷 P2414 [NOI2011] 阿狸的打字机 题解
  13. 2345看图王总是弹窗广告怎么办?
  14. 一路向往有光亮的地方-2015年终总结
  15. QT学习Github地址,git使用记录
  16. 三色旗的C语言算法,从算法学起C语言--三色旗问题
  17. C语言:一个球从100m高度自由落下,每次落地后反跳回原高度的一半,再落下,再反弹;求它在第10次落地时,共经过多少米,第10次反弹多高;
  18. 网贷逾期可怕吗?如果通知到朋友会怎么样?
  19. conda 克隆环境及导入新环境/conda环境移植
  20. 单源最短路径(dijkstra)

热门文章

  1. 打造专业人才发展链条:基于任职资格的专业人才发展与认证体系
  2. Jenkins 学习总结(9)—— Jenkins 有哪些替代方案?
  3. 中国金融体系主要指标大全!
  4. 消息中间件学习总结(1)——RocketMQ之专访RocketMQ联合创始人:项目思路、技术细节和未来规划
  5. 项目管理学习总结(2)——需求收集和管理
  6. linux ppp 串口 gprs,linux下GPRS ppp拨号默认路由问题(存在eth0)
  7. java源程序编译型_Java语言的源程序不是编译型的,而是编译解释型的
  8. java singleton inner class_Java面向对象设计模式-单例模式
  9. LInux下centos6.7 设置字符集,解决乱码问题
  10. mysql之grant权限说明