TestNG | Add and control test failure retries

With TestNG, you can create Retry Listeners and annotations that set the number of times a failed test should run again. The following Retry Class lets you specify how many times TestNG will run a failed test again with the maxRetryCount variable. Best practice is to specify the maxRetryCount with a parameter from the TestNG xml or a property file. The following example shows code that reruns the same failed test up to 5 times.

Copy
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class Retry implements IRetryAnalyzer {
    private int retryCount = 0;
    public boolean retry(ITestResult result) {
        int maxRetryCount = 5;
        if (retryCount < maxRetryCount) {
            retryCount++;
            System.out.println("Retry #" + retryCount + " for test: " + result.getMethod().getMethodName() + ", on thread: " + Thread.currentThread().getName());
            return true;
        }
        return false;
    }
}

The following RetryListener class overrides the TestNG annotation functionality so that every test will have the Retry annotation functionality and no additional class or test level retry annotations are necessary.

Copy
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
public class RetryListener implements IAnnotationTransformer {
    public RetryListener() {
    }
    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null)    {
            annotation.setRetryAnalyzer(Retry.class);
        }
    }
}

The following example shows a TestNG.xml file that adds the RetryListener inside the listener tag so the functionality in the previous example is utilized.

Copy
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite">
   <listeners>
      <listener class-name="your.namespace.RetryListener"/>  
   </listeners>