Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

LenarBad commented Nov 14, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.