How To Retry Failed Tests using IRetryAnalyzer in TestNG

  

The IRetryAnalyzer interface in TestNG allows us to automatically retry failed test cases. It is useful when tests may fail due to temporary issues like network problems, timeouts, or environment instability.


Steps to Implement IRetryAnalyzer

  • Create a class that implements IRetryAnalyzer.
  • Override the method boolean retry(ITestResult result).
  • Track retry count and limit the maximum number of retries.
  • Attach the RetryAnalyzer to test cases using:
    • @Test(retryAnalyzer = RetryAnalyzerClass.class), or

    • Globally using IAnnotationTransformer.




Example 1: RetryAnalyzer Implementation


import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer {
    private int count = 0;
    private static final int MAX_RETRY = 3;

    @Override
    public boolean retry(ITestResult result) {
        if (count < MAX_RETRY) {
            count++;
            return true;  // Retry the test
        }
        return false;  // Do not retry
    }
}




Example 2: Use RetryAnalyzer with Test


import org.testng.Assert;
import org.testng.annotations.Test;

public class SampleTest {

    int i = 0;

    @Test(retryAnalyzer = RetryAnalyzer.class)
    public void testMethod() {
        i++;
        System.out.println("Attempt #" + i);
        Assert.assertTrue(i >= 3, "Test failed");  // Passes on 3rd attempt
    }
}



Optional: Global Retry with IAnnotationTransformer

Instead of adding retryAnalyzer to each @Test, you can apply it globally:



RetryTransformer.java

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);
    }
}




Add Listener in testng.xml

<listeners>
    <listener class-name="RetryTransformer"/>
</listeners>



Important Points

ComponentPurpose
IRetryAnalyzerDefines retry logic for failed test cases
retry()Decides whether to retry a test
@Test(retryAnalyzer = ...)Attach retry logic to specific test
IAnnotationTransformerApply retry logic globally