Back to All Scenarios
Scenario 55 of 100
Framework Design
Advanced
Implementing Auto-Retry for Flaky Tests
πScenario Overview
Implementing Auto-Retry for Flaky Tests
Key Takeaways & Cheat Sheet
- βIdentify causes of flaky tests (network blips, server delay, timing lags)
- βImplement TestNG IRetryAnalyzer interface to govern repeat executions
- βImplement IAnnotationTransformer to apply retry rules globally to all tests
- βAvoid setting massive retry limits that inflate test execution times
Short Direct Answer
To prevent false alarms in CI/CD pipelines caused by temporary network latency or slow API responses, you can implement an auto-retry mechanism. For TestNG, create a class implementing `IRetryAnalyzer` to rerun failed tests a defined number of times (typically 1-2) before marking them as failed.
β οΈ Senior Warning (Red Flag)
Never set high retry limits (e.g. retrying a test 5 times). High retry limits mask real performance bugs and significantly slow down your CI/CD execution pipeline.
π‘ STAR Deep Dive Explanation & Pro Tip
Apply this analyzer globally by writing a custom TestNG annotation transformer. This guarantees retry behaviors are active across the entire test suite automatically.
SeleniumAutomation.java
Selenium 4 + Javaimport org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
// β
Custom Retry Analyzer implementation
public class RetryAnalyzer implements IRetryAnalyzer {
private int retryCount = 0;
private static final int MAX_RETRY_LIMIT = 2; // Retry failed tests up to 2 times
@Override
public boolean retry(ITestResult result) {
if (retryCount < MAX_RETRY_LIMIT) {
retryCount++;
System.out.println("Retrying failed test: " + result.getName() + " | Attempt: " + retryCount);
return true; // Rerun the test
}
return false; // Stop retrying and mark test as failed
}
}