You can use the IRetryAnalyzer interface provided by testng for this. Using this you can retry your failed test cases multiple times till it passes. In the below example Ithe max retry count is set to 1.
public class Retry implements IRetryAnalyzer{
int retryCount =0;
int maxRetryCount = 1;
public boolean retry(ITestResult result) {
// TODO Auto-generated method stub
if(retryCount < maxRetryCount){
System.out.println("Retrying test with "+result.getName()+" with status "+
getResultStatusName(result.getStatus()));
retryCount++;
return true;
}
return false;
}
public String getResultStatusName(int status){
String resultName = null;
if(status==1)
resultName ="SUCCESS";
if(status==2)
resultName = "FAILURE";
if(status==3)
resultName = "SKIP";
return resultName;
}
}
public class RetryListner implements IAnnotationTransformer{
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
// TODO Auto-generated method stub
IRetryAnalyzer analyzer = annotation.getRetryAnalyzer();
if(analyzer ==null){
annotation.setRetryAnalyzer(Retry.class);
}
}
}
And add the listener to the testng.xml
<listener
class-name="com.listeners.RetryListner">
</listener>