A Guide to @RepeatedTest in Junit 5 – Junit 5中的@RepeatedTest指南

最后修改: 2017年 5月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick article, we are going to look at the @RepeatedTest annotation introduced in JUnit 5. It provides us a powerful way to write any test that we want to repeat several times.

在这篇快速的文章中,我们要看一下JUnit 5中引入的@RepeatedTest注解。它为我们提供了一种强大的方式来编写任何我们想要重复多次的测试。

If you want to learn more about JUnit 5, please check our other articles explaining the basics and guide to JUnit 5.

如果您想进一步了解JUnit 5,请查看我们的其他文章解释基础知识JUnit 5指南

2. Maven Dependencies and Setup

2.Maven的依赖性和设置

The first thing to note is that JUnit 5 needs Java 8 to run. Having said that, let’s have a look at the Maven dependency:

首先要注意的是,JUnit 5需要Java 8才能运行。说了这么多,让我们看看Maven的依赖性。

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

This is the main JUnit 5 dependency that we need to add to write our tests. Check out the latest version of the artifact here.

这是我们在编写测试时需要添加的主要JUnit 5依赖项。请查看该工件的最新版本这里

3. A Simple @RepeatedTest Example

3.一个简单的@RepeatedTest例子

Creating a repeated test is simple – just add the @RepeatedTest annotation on top of the test method:

创建一个重复测试很简单–只要在测试方法的顶部添加@RepeatedTest注解。

@RepeatedTest(3)
void repeatedTest(TestInfo testInfo) {
    System.out.println("Executing repeated test");
 
    assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}

Note that instead of standard @Test annotation, we are using @RepeatedTest for our unit test. The above test will be executed three times as if the same test was written three times.

注意,我们没有使用标准的@Test注解,而是使用@RepeatedTest进行单元测试。上述测试将被执行三次,就好像同一个测试被写了三次。

The test reports (the report files or the results in the JUnit tab of your IDE) will display all the executions:

测试报告(报告文件或你的IDE的JUnit标签中的结果)将显示所有的执行情况。

repetition 1 of 3(repeatedTest(TestInfo))
repetition 2 of 3(repeatedTest(TestInfo))
repetition 3 of 3(repeatedTest(TestInfo))

4. Lifecycle Support for @RepeatedTest

4.对@RepeatedTest的生命周期支持

Each execution of the @RepeatedTest will behave like a regular @Test having full JUnit test life cycle support. Meaning that, during each execution, the @BeforeEach and @AfterEach methods will be called. To demonstrate this, just add the appropriate methods in the test class:

@RepeatedTest的每一次执行都会像普通的@Test一样具有完整的JUnit测试生命周期支持。这意味着,在每次执行期间,@BeforeEach@AfterEach方法将被调用。为了证明这一点,只需在测试类中添加适当的方法。

@BeforeEach
void beforeEachTest() {
    System.out.println("Before Each Test");
}

@AfterEach
void afterEachTest() {
    System.out.println("After Each Test");
    System.out.println("=====================");
}

If we run our previous test, the results will be displayed on the console:

如果我们运行我们之前的测试,结果将显示在控制台。

Before Each Test
Executing repeated test
After Each Test
=====================
Before Each Test
Executing repeated test
After Each Test
=====================
Before Each Test
Executing repeated test
After Each Test
=====================

As we can see, the @BeforeEach and @AfterEach methods are called around each execution.

我们可以看到,@BeforeEach@AfterEach方法在每次执行时都被调用

5. Configuring the Test Name

5.配置测试名称

In the first example, we have observed that the output of the test report does not contain any identifiers. This can be configured further using the name attribute:

在第一个例子中,我们已经观察到,测试报告的输出不包含任何标识符。这可以使用name属性来进一步配置。

@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
void repeatedTestWithLongName() {
    System.out.println("Executing repeated test with long name");
 
    assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}

The output will now contain the method name along with the repetition index:

现在的输出将包含方法名称和重复索引。

repeatedTestWithLongName() :: repetition 1 of 3(repeatedTestWithLongName())
repeatedTestWithLongName() :: repetition 2 of 3(repeatedTestWithLongName())
repeatedTestWithLongName() :: repetition 3 of 3(repeatedTestWithLongName())

Another option is to use RepeatedTest.SHORT_DISPLAY_NAME which will produce the short name of the test:

另一个选择是使用RepeatedTest.SHORT_DISPLAY_NAME,这将产生测试的短名称。

repetition 1 of 3(repeatedTestWithShortName())
repetition 2 of 3(repeatedTestWithShortName())
repetition 3 of 3(repeatedTestWithShortName())

If however, we need to use our customized name, it is very much possible:

然而,如果我们需要使用我们的定制名称,这是很有可能的。

@RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}")
void repeatedTestWithCustomDisplayName(TestInfo testInfo) {
    assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}

The {currentRepetition} and {totalRepetitions} are the placeholders for the current repetition and the total number of repetitions. These values are automatically provided by JUnit at the runtime, and no additional configuration is required. The output is pretty much what we expected:

{currentRepetition}{totalRepetitions}是当前重复次数和总重复次数的占位符。这些值是由JUnit在运行时自动提供的,不需要额外的配置。输出结果与我们预期的差不多。

Custom name 1/3(repeatedTestWithCustomDisplayName())
Custom name 2/3(repeatedTestWithCustomDisplayName())
Custom name 3/3(repeatedTestWithCustomDisplayName())

6. Accessing the RepetitionInfo

6.访问RepetitionInfo

Apart from the name attribute, JUnit provides access to the repetition metadata in our test code as well. This is achieved by adding a RepetitionInfo parameter to our test method:

除了name属性外,JUnit还提供了对我们测试代码中重复元数据的访问。这是通过向我们的测试方法添加一个RepetitionInfo参数来实现的。

@RepeatedTest(3)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
    System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
 
    assertEquals(3, repetitionInfo.getTotalRepetitions());
}

The output will contain the current repetition index for each of the execution:

输出将包含每个执行的当前重复索引。

Repetition #1
Repetition #2
Repetition #3

The RepetitionInfo is provided by RepetitionInfoParameterResolver and is available only in the context of @RepeatedTest.

RepetitionInfoRepetitionInfoParameterResolver提供,并且只在@RepeatedTest.的背景下可用。

7. Conclusion

7.结论

In this quick tutorial, we explored the @RepeatedTest annotation provided by JUnit and learned different ways of configuring it.

在这个快速教程中,我们探讨了JUnit提供的@RepeatedTest注解,并学习了配置它的不同方法。

Don’t forget to check out the full source code for this article over on GitHub.

不要忘记在GitHub上查看本文的完整的源代码