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:
<code class="java">public class RetryTest { public class RetryRule implements TestRule { ... public Statement apply(Statement base, Description description) { ... } ... } }</code>
In the apply method, insert your retry logic around the test call using the provided base.evaluate():
<code class="java">public Statement apply(Statement, Description) { return new Statement() { @Override public void evaluate() throws Throwable { ... for (int i = 0; i < retryCount; i++) { ... } ... } }; }</code>
Annotate your test class with the Rule like this:
<code class="java">... @Rule public Retry rule = new Retry(3); ...</code>
Custom TestRunner:
Alternatively, you can create a custom TestRunner that extends BlockJUnit4ClassRunner and overrides runChild() to implement the retry mechanism.
Note:
The above is the detailed content of How to Retry Failed JUnit Tests Instantly?. For more information, please follow other related articles on the PHP Chinese website!