Test automation failures can happen due to temporary issues such as network problems, application delays, or environment instability. Instead of manually rerunning failed tests, TestNG provides a retry mechanism that can automatically execute failed test cases again. In this article, we will learn how to implement retry logic in TestNG using Java and the built-in IRetryAnalyzer interface.
What Is Retry Logic in TestNG?
Retry logic allows a failed test case to run again automatically before marking it as a final failure.
For example:
- Test fails during the first execution
- TestNG retries the test automatically
- Test passes on the second attempt
- Execution continues successfully
Retry logic is commonly used for handling:
- Temporary network failures
- Browser issues
- Application response delays
- Environment instability
However, retries should not hide real defects. Always investigate the root cause of repeated failures.
Why Use Retry Logic in Test Automation?
In CI/CD pipelines, tests may fail due to temporary conditions rather than actual application bugs.
Common examples:
- API response timeout
- Server performance issues
- Browser crash
- Network interruption
- Selenium synchronization problems
A retry mechanism helps reduce false failures and improves pipeline stability.
How Does TestNG Retry Work?
TestNG provides the IRetryAnalyzer interface for implementing retry logic.
The interface contains one method:
boolean retry(ITestResult result);
This method decides whether a failed test should run again.
Return values:
true→ Retry the testfalse→ Do not retry
Step 1: Create Retry Analyzer Class
Create a class that implements IRetryAnalyzer.
import org.testng.IRetryAnalyzer;import org.testng.ITestResult;public class RetryAnalyzer implements IRetryAnalyzer { private int retryCount = 0; private static final int maxRetryCount = 2; @Override public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { retryCount++; return true; } return false; }}
Explanation
In this example:
- Maximum retry count is set to 2
- A failed test can execute up to 3 times:
- First execution
- Retry 1
- Retry 2
After reaching the limit, TestNG marks the test as failed.
Step 2: Apply Retry Analyzer to Test Case
Use the retryAnalyzer attribute in the @Test annotation.
Example:
import org.testng.Assert;import org.testng.annotations.Test;public class LoginTest { @Test(retryAnalyzer = RetryAnalyzer.class) public void verifyLogin() { System.out.println("Executing login test"); Assert.fail("Test failed"); }}
When this test fails, TestNG automatically calls the retry analyzer.
Execution flow:
Test Execution | ↓Test Failed | ↓RetryAnalyzer Called | ↓Retry Available? | Yes -----> Execute Test Again | No -----> Mark Test Failed
Implement Retry Logic Globally for All Tests
Adding retryAnalyzer to every test method can become repetitive.
A better approach is using IAnnotationTransformer.
This allows retry logic to be applied automatically to all tests.
Step 3: Create Annotation Transformer
import org.testng.IAnnotationTransformer;import org.testng.annotations.ITestAnnotation;import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class RetryTransformer implements IAnnotationTransformer { @Override public void transform( ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { annotation.setRetryAnalyzer(RetryAnalyzer.class); }}
Step 4: Register Transformer in testng.xml
Add the listener configuration:
<suite name="Automation Suite"> <listeners> <listener class-name="RetryTransformer"/> </listeners> <test name="Regression Tests"> <classes> <class name="LoginTest"/> </classes> </test></suite>
Now every failed test will automatically use the retry mechanism.
Adding Logging During Retry
It is useful to know which tests are being retried.
Update the retry analyzer:
import org.testng.IRetryAnalyzer;import org.testng.ITestResult;public class RetryAnalyzer implements IRetryAnalyzer { private int retryCount = 0; private static final int maxRetryCount = 2; @Override public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { retryCount++; System.out.println( "Retrying test: " + result.getName() + " Attempt: " + retryCount ); return true; } return false; }}
Example output:
Executing login testRetrying test: verifyLogin Attempt: 1Retrying test: verifyLogin Attempt: 2
Retry Failed Tests Only for Specific Exceptions
Not every failure should be retried.
For example:
- Element timeout → Retry may help
- Assertion failure due to application bug → Retry may not help
Example:
@Overridepublic boolean retry(ITestResult result) { Throwable exception = result.getThrowable(); if (exception instanceof TimeoutException && retryCount < maxRetryCount) { retryCount++; return true; } return false;}
This approach prevents unnecessary retries.
Best Practices for TestNG Retry Logic
1. Do Not Use Too Many Retries
Avoid:
maxRetryCount = 10;
Multiple retries increase execution time and can hide real problems.
Usually:
1-2 retries
are sufficient.
2. Capture Screenshots During Retry
When a test fails, capture:
- Screenshot
- Browser console logs
- Test logs
- Failure reason
This helps identify whether the failure is temporary or a real defect.
3. Track Flaky Tests
A test that repeatedly requires retries should be investigated.
Maintain visibility using:
- Test reports
- CI dashboards
- Failure history
4. Fix Root Causes
Retry logic should improve stability, not replace good automation practices.
Always check:
- Wait strategies
- Locator stability
- Test data handling
- Environment reliability
Interview Answer: How Do You Implement Retry Logic in TestNG?
A good interview response:
“In TestNG, I implement retry logic using the IRetryAnalyzer interface. I create a custom retry analyzer class that controls the retry count and returns true when a failed test should be executed again. For applying retries across all tests, I use IAnnotationTransformer. Retry logic helps handle temporary failures, but I use it carefully and always investigate the root cause of flaky tests.”
Frequently Asked Questions
How many times should a failed TestNG test be retried?
Usually, one or two retries are enough. Higher retry counts can increase execution time and hide automation issues.
Does TestNG retry failed tests automatically?
No. TestNG requires a custom implementation using IRetryAnalyzer or a listener-based approach.
Can we retry only failed tests in TestNG?
Yes. IRetryAnalyzer is specifically designed to retry failed test methods.
Conclusion
Retry logic in TestNG is a useful technique for handling temporary automation failures. By implementing IRetryAnalyzer, teams can automatically rerun unstable tests and improve CI/CD reliability.
However, retries should be used as a safety mechanism, not as a replacement for fixing flaky tests. A stable automation framework requires proper waits, independent tests, reliable test data, and detailed reporting.
Related Articles
Discover more from Rotebit
Subscribe to get the latest posts sent to your email.
