重复测试

JUnit Jupiter提供了通过使用@RepeatedTest注释方法并指定所需重复次数的能力。重复测试的每次调用都表现为对相同生命周期回调和扩展的完全支持的常规@Test方法的执行。

下面的示例演示如何声明一个名为repeatedTest()的测试,它将自动重复10次。

  [java]
1
2
3
4
@RepeatedTest(10) void repeatedTest() { // ... }

特性说明

除了指定重复次数之外,还可以通过 @RepeatedTest 注释的name属性为每次重复配置自定义显示名。 此外,显示名称可以是由静态文本和动态占位符组合而成的模式。目前支持以下占位符。

{displayName}: @RepeatedTest方法的显示名称。

{currentRepetition}:当前重复计数

{总重复}:总重复次数

给定重复的默认显示名称是基于以下模式生成的: repetition {currentRepetition} of {totalRepetitions}。 因此,显示名称为个人重复以前的repeatedTest()的例子是: repetition 1 of 10, repetition 2 of 10,等等。 如果你想要显示的名称@RepeatedTest方法包含在每个重复的名称,您可以定义自己的自定义模式或使用预定义的 RepeatedTest.LONG_DISPLAY_NAME 模式。 后者等于{displayName}:repetition{totalrepeat},这导致了 repeatedTest() :: repetition 1 of 10repeatedTest() :: repetition 2 of 10,等等。

为了检索有关当前重复的信息和编程的总重复次数,开发人员可以选择将重复信息的实例注入到@RepeatedTest、@BeforeEach或@AfterEach方法中。

测试案例

本节末尾的RepeatedTestsDemo类演示了几个重复测试的示例。

repeatedTest()方法与前一节中的示例相同; 然而,repeatedtestwithtioninfo()演示了如何将一个 RepetitionInfo 实例注入到测试中,以访问当前重复测试的总重复次数。

接下来的两个方法演示了如何在每次重复的显示名中包含@RepeatedTest方法的自定义@DisplayName。 customDisplayName()将自定义显示名称与自定义模式组合在一起,然后使用TestInfo来验证生成的显示名称的格式。 Repeat! 是来自@DisplayName声明的{displayName}, 而1/1来自{currentrepeat}/{totalrepeat}。 相反,customDisplayNameWithLongPattern()使用前面提到的预定义 RepeatedTest.LONG_DISPLAY_NAME 模式。

repeatedTestInGerman()演示了将重复测试的名称翻译成外语的能力——在本例中是德语,导致了单个重复的名称, 比如:

  [plaintext]
1
Wiederholung 1 von 5, Wiederholung 2 von 5

因为beforeEach()方法是用@BeforeEach注释的,所以每次重复测试之前都会执行它。 通过将TestInfo和repeat tioninfo注入到方法中,我们可以获得关于当前正在执行的重复测试的信息。 在下面的输出中,使用信息日志级别启用的结果执行RepeatedTestsDemo。

  • 日志
  [plaintext]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 1 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 2 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 3 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 4 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 5 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 6 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 7 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 8 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 9 of 10 for repeatedTest Jun 25, 2018 7:09:09 PM com.github.houbb.jdk.junit5.repeat.RepeatedTestsDemo beforeEach 信息: About to execute repetition 10 of 10 for repeatedTest

实例

  • RepeatedTestsDemo.java
  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import static org.junit.jupiter.api.Assertions.assertEquals; 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.TestInfo; import java.util.logging.Logger; public class RepeatedTestsDemo { Logger logger = Logger.getLogger(RepeatedTestsDemo.class.getName()); @BeforeEach void beforeEach(TestInfo testInfo, RepetitionInfo repetitionInfo) { int currentRepetition = repetitionInfo.getCurrentRepetition(); int totalRepetitions = repetitionInfo.getTotalRepetitions(); String methodName = testInfo.getTestMethod().get().getName(); logger.info(String.format("About to execute repetition %d of %d for %s", // currentRepetition, totalRepetitions, methodName)); } @RepeatedTest(10) void repeatedTest() { // ... } @RepeatedTest(5) void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) { assertEquals(5, repetitionInfo.getTotalRepetitions()); } @RepeatedTest(value = 1, name = "{displayName} {currentRepetition}/{totalRepetitions}") @DisplayName("Repeat!") void customDisplayName(TestInfo testInfo) { assertEquals(testInfo.getDisplayName(), "Repeat! 1/1"); } @RepeatedTest(value = 1, name = RepeatedTest.LONG_DISPLAY_NAME) @DisplayName("Details...") void customDisplayNameWithLongPattern(TestInfo testInfo) { assertEquals(testInfo.getDisplayName(), "Details... :: repetition 1 of 1"); } @RepeatedTest(value = 5, name = "Wiederholung {currentRepetition} von {totalRepetitions}") void repeatedTestInGerman() { // ... } }