In software testing service you can achieve this using multiple ways. One of the way is as follow.
Write a code in your class that implement 'IRetryAnalyzer' just as below.
package com.mavericks.qa.RetryListener;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryFailedTestCases implements IRetryAnalyzer {
private int retryCnt = 0;
//You could mentioned maxRetryCnt (Maximiun Retry Count) as per your requirement. Here I took 2, If any failed testcases then it runs two times
private int maxRetryCnt = 2;
//This method will be called everytime a test fails. It will return TRUE if a test fails and need to be retried, else it returns FALSE
public boolean retry(ITestResult result) {
if (retryCnt < maxRetryCnt) {
System.out.println("Retrying " + result.getName() + " again and the count is " + (retryCnt+1));
retryCnt++;
return true;
}
return false;
}
}
And another file 'RetryListenerClass' having code like as below
package com.mavericks.qa.RetryListener;
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 RetryListenerClass implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
if (retry == null) {
testannotation.setRetryAnalyzer(RetryFailedTestCases.class);
}
}
}
Now just add below lines in your testng file before the test Extent listner
<listener
class-name="com.mavericks.qa.RetryListener.RetryListenerClass" />