Trying to handle infrequent test failures due to time-sensitive actions in large test suites can be frustrating. The good news is that you can implement a TestRule to retry failed tests.
A TestRule gives you control over test execution. To create a RetryRule, define a class like this:
public class RetryTest {
public class RetryRule implements TestRule {
...
public Statement apply(Statement base, Description description) {
...
}
...
}
}
In the apply method, insert your retry logic around the test call using the provided base.evaluate():
public Statement apply(Statement, Description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
...
for (int i = 0; i Using the RetryRule
Annotate your test class with the Rule like this:
...
@Rule
public Retry rule = new Retry(3);
...
Other Options
Custom TestRunner:
Alternatively, you can create a custom TestRunner that extends BlockJUnit4ClassRunner and overrides runChild() to implement the retry mechanism.
Note:
- The RetryRule allows you to specify the number of retries, while the custom TestRunner does not.
- You can also use annotations to apply the retry logic selectively.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3