文
章
目
录
章
目
录
这段文本主要描述了JUnit框架中的@RepeatedTest注解,它用于编写可重复运行的测试模板。@RepeatedTest注解可以配置重复频率作为参数。
1.@RepeatedTest注解
@RepeatedTest注解用于标记需要重复特定次数并且可配置显示名称的测试方法。
如果想要用不同的参数重复测试,建议使用@ParameterizedTest注解。
1.1 语法
要创建可重复的测试,给测试方法添加@RepeatedTest注解。
在给定的示例中,测试方法使用了@RepeatedTest(5)注解,意味着该测试将执行五次。
@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");
}
1.2 生命周期方法
请注意,每次重复测试的行为都类似于常规测试的执行,并完全支持相同的生命周期回调和扩展。这意味着@BeforeEach和@AfterEach注解的生命周期方法将针对每次测试调用进行调用。
@RunWith(JUnitPlatform.class)
public class RepeatedTestExample {
@BeforeAll
public static void init(){
System.out.println("Before All init() method called");
}
@BeforeEach
public 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");
}
@AfterEach
public void cleanUpEach(){
System.out.println("After Each cleanUpEach() method called");
}
@AfterAll
public static void cleanUp(){
System.out.println("After All cleanUp() method called");
}
}
上述测试的输出:
Before All init() method called
Before Each initEach() method called
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 1
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 2
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 3
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 4
After Each cleanUpEach() method called
Before Each initEach() method called
Running addNumber test -> 5
After Each cleanUpEach() method called
After All cleanUp() method called
2.自定义显示名称
除了指定重复次数外,还可以为每个重复提供自定义显示名称,这个自定义显示名称可以是{static text + dynamic placeholders}的组合。
目前,支持三种占位符:
- {displayName}:@RepeatedTest方法的显示名称。
- {currentRepetition}:当前重复计数。
- {totalRepetitions}:总的重复次数。
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");
}
}
运行上述测试将输出以下内容:我们可以使用两种预定义格式之一,即LONG_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)中的重复信息。
public class JUnit5AnnotationsExample {
@BeforeEach
public 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");
}
@AfterEach
public void cleanUpEach(RepetitionInfo info){
int currentRepetition = info.getCurrentRepetition();
int totalRepetitions = info.getTotalRepetitions();
//Use information as needed
}
}