Skip to content

Instantly share code, notes, and snippets.

@LenarBad
Last active November 14, 2017 07:05
Show Gist options
  • Save LenarBad/47d0514f65c59916583d09830df1aace to your computer and use it in GitHub Desktop.
Save LenarBad/47d0514f65c59916583d09830df1aace to your computer and use it in GitHub Desktop.
How to re-run failed tests in TestNG using IRetryAnalyzer
import org.joda.time.DateTime;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer {
private int retryCount = 0;
private static int maxNumberOfRetriesForUnstableTests = 2;
private static int maxNumberOfRetriesForVeryUnstableTests = 3;
private static int relaxDayMaxNumberOfRetries = 10;
private static String rushDay = "Monday";
private static String relaxDay = "Thursday";
public boolean retry(ITestResult result) {
retryCount++;
if (isRushDayToday()) return false;
if (isRelaxDayToday() && retryCount <= relaxDayMaxNumberOfRetries) return true;
if (retryCount <= maxNumberOfRetriesForVeryUnstableTests && result.getName().equals("veryUnstableTest")) return true;
if (retryCount <= maxNumberOfRetriesForUnstableTests) return true;
return false;
}
private boolean isRushDayToday() {
return new DateTime().dayOfWeek().getAsText().equals(rushDay);
}
private boolean isRelaxDayToday() {
return new DateTime().dayOfWeek().getAsText().equals(relaxDay);
}
}
import org.testng.annotations.Test;
public class RetryExampleTest {
@Test
public void stableTest() {
// some test code
}
@Test(retryAnalyzer = RetryAnalyzer.class)
public void unstableTest() {
// some test code
}
@Test(retryAnalyzer = RetryAnalyzer.class)
public void veryUnstableTest() {
// some test code
}
}
import org.testng.annotations.Test;
public class RetryExampleTest {
@Test
public void stableTest() {
// some test code
}
@Test(retryAnalyzer = RetryAnalyzer.class)
public void unstableTest() {
// some test code
}
@Test(retryAnalyzer = RetryAnalyzer.class)
public void veryUnstableTest() {
// some test code
}
}
@LenarBad
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment