Last active
January 29, 2021 16:41
-
-
Save naelabdeljawad/f47f125870e13c9472253b84e389adad to your computer and use it in GitHub Desktop.
TestNG Retry Test Execution by RetryListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.testng.IRetryAnalyzer; | |
import org.testng.ITestResult; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Method; | |
public class Retry implements IRetryAnalyzer { | |
private int retryCount = 0; | |
private int maxRetryCount = 1; | |
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; | |
} | |
/* | |
* Below method returns 'true' if the test method has to be retried else | |
* 'false' and it takes the 'Result' as parameter of the test method that | |
* just ran | |
* | |
* @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult) | |
*/ | |
@Override | |
public boolean retry(ITestResult result) { | |
if (retryCount < maxRetryCount) { | |
System.out.println("Retrying test " + result.getName() + " with status " + getResultStatusName(result.getStatus()) + " for the " + (retryCount + 1) + " time(s)."); | |
retryCount++; | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Add below class as a new class | |
*/ | |
public class RetryListener implements IAnnotationTransformer { | |
@SuppressWarnings("rawtypes") | |
@Override | |
public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, | |
Method testMethod) { | |
IRetryAnalyzer retry = testannotation.getRetryAnalyzer(); | |
if (retry == null) { | |
testannotation.setRetryAnalyzer(Retry.class); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment