繁体   English   中英

Junit5 的重复注解

[英]Repeat annotation for Junit5

您将如何为 Junit5 构建 @Repeat 注释?

此外,我希望该选项能够将重复测试报告为 IDE 中的一项测试。

每个重复应该能够以某种方式记录其数量,以便您可以看到进度。

从 M4 开始,JUnit Jupiter 为重复测试提供一流的支持:

@RepeatedTest(10)
void repeatedTest() {
    // ...
}

有关文档和其他示例,请参阅用户指南

我发现通常当您想重试整个测试时,最好在测试中识别并重试不稳定的操作。

例如,想象一下在下面的代码中getResponse()有时会抛出异常。 您可以使用Spring Retry (您可以使用或不使用其他 Spring 项目):

@Test
void somethingFlaky() {
    String actual = new RetryTemplate().execute(retryContext -> getResponse());
    assertEquals("Hello World!", actual);
}

在此示例中,如果getResponse()连续抛出异常 3 次,则整个测试将失败,并在最后一次尝试中抛出异常。 RetryTemplate可以配置各种不同的重试和回退策略(例如重试 8 次、每次尝试之间休眠 50 毫秒、指数回退等)。

通过这种方式,您可以根据需要管理重试资源,并让测试框架做它最擅长的事情:管理测试(而不是不稳定的测试目标)。

@RepeatedTest现在在 JUnit 5 中可用。您需要的所有内容现在都已实现。

您可以使用 @RepeatedTest 注释。 由于没有给出任何代码示例,请参阅下面的代码示例以获得清晰的理解。

@RepeatedTest(3)
void testsum(RepetitionInfo repetitionInfo){
    //now you access the information for the repetition
    repetitionInfo.getCurrentRepetition(); //Get the current repetition of the corresponding @RepeatedTest method.

    repetitionInfo.getTotalRepetitions(); //Get the total number of repetitions of the corresponding @RepeatedTest method.


}

目前没有办法按照您的要求做,因为没有允许灵活创建测试的扩展点。 但是您也许可以使用产生动态测试的实用方法来做一些事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM