Junior — Middle
How can a flaky test be rerun to eliminate false failures?
sobes.tech AI
Answer from AI
To rerun an unstable test (flaky test), retries are often used. This can be implemented at the level of the test framework or CI/CD:
- In JUnit, using rules or annotations, for example,
@Retry. - In TestNG, there is built-in support for reruns via
IRetryAnalyzer. - In CI/CD, you can configure reruns for failed tests.
Example in TestNG:
public class RetryAnalyzer implements IRetryAnalyzer {
private int count = 0;
private static final int maxRetry = 3;
@Override
public boolean retry(ITestResult result) {
if (count < maxRetry) {
count++;
return true;
}
return false;
}
}
@Test(retryAnalyzer = RetryAnalyzer.class)
public void unstableTest() {
// test that can sometimes fail
}
This approach helps reduce the impact of false failures, but it is also important to identify and eliminate the causes of instability.