简体   繁体   中英

Repeat annotation for Junit5

How would you build a @Repeat annotation for Junit5?

Also, i'd want the option to be able to have a repeated test reported as one test in the IDE.

And each repeated should be able to log its number in some way, so you can see progress.

Since M4, JUnit Jupiter provides first-class support for repeated tests:

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

Please refer to the User Guide for documentation and additional examples.

I find that normally when you want to retry an entire test that it is better to identify and retry the flaky operation(s) within the test.

eg Imagine that in the following code getResponse() sometimes throws an exception. You can use Spring Retry (which you can use with or without other Spring Projects):

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

In this example, if getResponse() throws exceptions 3 times in a row then the overall test will fail with the exception thrown in the last attempt. RetryTemplate can be configured with various different retry and back off policies (eg retry 8 times, sleep 50 milliseconds between each attempt, exponential back off, etc.).

This way you can manage retrying resources as needed and leave the test framework to do what it does best: manage tests (and not flaky test targets).

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

You can use the @RepeatedTest annotation. Since there are no any code sample given, please refer below code sample for clear understanding.

@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.


}

There is currently no way to do what you ask as there is no extension point that allows the flexible creation of tests. But you might be able to do something with a utility method that spawns dynamic tests .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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