Repeated tests

JUnit Jupiter provides for the ability to repeat a test a specified number of times simply by annotating a method with @RepeatedTest, specifying the total number of repetitions desired. Each repeated test behaves exactly as a regular @Test method. Moreover, each repeated test preserves the same lifecycle callbacks (@BeforeEach, @AfterEach, and so on).

The following Java class contains a test that is going to be repeated five times:

package io.github.bonigarcia;

import org.junit.jupiter.api.RepeatedTest;

class SimpleRepeatedTest {

@RepeatedTest(5)
void test() {
System.out.println("Repeated test");
}

}

Due to the fact that this test only writes a line (Repeated test) in the standard output, when executing this test in the console, we will see that trace five times:

Execution of repeated test in the console

In addition to specifying the number of repetitions, a custom display name can be configured for each repetition via the name attribute of the @RepeatedTest annotation. The display name can be a pattern composed of a combination of static text and dynamic placeholders. The following are currently supported:

  • {displayName}: This is the name of the @RepeatedTest method.
  • {currentRepetition}: This is the current repetition count.
  • {totalRepetitions}: This is the total number of repetitions.

The following example shows a class with three repeated tests in which the display name is configured with the property name of @RepeatedTest:

package io.github.bonigarcia;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.TestInfo;

class TunningDisplayInRepeatedTest {

@RepeatedTest(value = 2, name = "{displayName}
{currentRepetition}/{totalRepetitions}")
@DisplayName("Repeat!")
void customDisplayName(TestInfo testInfo) {
System.out.println(testInfo.getDisplayName());
}

@RepeatedTest(value = 2, name = RepeatedTest.LONG_DISPLAY_NAME)
@DisplayName("Test using long display name")
void customDisplayNameWithLongPattern(TestInfo testInfo) {
System.out.println(testInfo.getDisplayName());
}

@RepeatedTest(value = 2, name = RepeatedTest.SHORT_DISPLAY_NAME)
@DisplayName("Test using short display name")
void customDisplayNameWithShortPattern(TestInfo testInfo) {
System.out.println(testInfo.getDisplayName());
}

}

In this test, the display name for these repeated tests will be as follows:

  • For the test customDisplayName, the display name will follow the long display format:
    • Repeat 1 out of 2.
    • Repeat 2 out of 2.
  • For the test customDisplayNameWithLongPattern, the display name will follow the long display format:
    • Repeat! 1/2.
    • Repeat! 2/2.
  • For the test customDisplayNameWithShortPattern, the display name in this test will follow the short display format:
    • Test using long display name :: repetition 1 of 2.
    • Test using long display name :: repetition 2 of 2.
Execution of repeated test example in conjunction with @DisplayName
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.37.250