原文: https://howtodoinjava.com/junit5/repeated-test-annotation-example/

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

1. @RepeatedTest注解用法

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

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

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

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

  1. package com.howtodoinjava.junit5.examples;
  2. import org.junit.jupiter.api.AfterAll;
  3. import org.junit.jupiter.api.AfterEach;
  4. import org.junit.jupiter.api.Assertions;
  5. import org.junit.jupiter.api.BeforeAll;
  6. import org.junit.jupiter.api.BeforeEach;
  7. import org.junit.jupiter.api.DisplayName;
  8. import org.junit.jupiter.api.RepeatedTest;
  9. import org.junit.jupiter.api.RepetitionInfo;
  10. import org.junit.jupiter.api.Test;
  11. import org.junit.jupiter.api.TestInfo;
  12. import org.junit.platform.runner.JUnitPlatform;
  13. import org.junit.runner.RunWith;
  14. @RunWith(JUnitPlatform.class)
  15. public class RepeatedTestExample {
  16. @BeforeAll
  17. public static void init(){
  18. System.out.println("Before All init() method called");
  19. }
  20. @BeforeEach
  21. public void initEach(){
  22. System.out.println("Before Each initEach() method called");
  23. }
  24. @DisplayName("Add operation test")
  25. @RepeatedTest(5)
  26. void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo)
  27. {
  28. System.out.println("Running addNumber test -> " + repetitionInfo.getCurrentRepetition());
  29. Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
  30. }
  31. @AfterEach
  32. public void cleanUpEach(){
  33. System.out.println("After Each cleanUpEach() method called");
  34. }
  35. @AfterAll
  36. public static void cleanUp(){
  37. System.out.println("After All cleanUp() method called");
  38. }
  39. }

上面程序的输出:

  1. Before All init() method called
  2. Before Each initEach() method called
  3. After Each cleanUpEach() method called
  4. Before Each initEach() method called
  5. Running addNumber test -> 1
  6. After Each cleanUpEach() method called
  7. Before Each initEach() method called
  8. Running addNumber test -> 2
  9. After Each cleanUpEach() method called
  10. Before Each initEach() method called
  11. Running addNumber test -> 3
  12. After Each cleanUpEach() method called
  13. Before Each initEach() method called
  14. Running addNumber test -> 4
  15. After Each cleanUpEach() method called
  16. Before Each initEach() method called
  17. Running addNumber test -> 5
  18. After Each cleanUpEach() method called
  19. After All cleanUp() method called

2. 自定义显示测试名称

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

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

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

JUnit5 `@RepeatedTest`注解示例 - 图1

JUnit5 重复测试的显示名称

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

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

3. RepetitionInfo接口

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

  1. @RunWith(JUnitPlatform.class)
  2. public class JUnit5AnnotationsExample {
  3. @BeforeEach
  4. public void initEach(RepetitionInfo info){
  5. int currentRepetition = info.getCurrentRepetition();
  6. int totalRepetitions = info.getTotalRepetitions();
  7. //Use information as needed
  8. }
  9. @DisplayName("Add operation test")
  10. @RepeatedTest(value = 5, name="{displayName} :: repetition {currentRepetition} of {totalRepetitions}")
  11. void addNumber(TestInfo testInfo) {
  12. Calculator calculator = new Calculator();
  13. Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
  14. }
  15. @AfterEach
  16. public void cleanUpEach(RepetitionInfo info){
  17. int currentRepetition = info.getCurrentRepetition();
  18. int totalRepetitions = info.getTotalRepetitions();
  19. //Use information as needed
  20. }
  21. }

将我的问题放在评论部分。

学习愉快!

源码下载