retrylogic

20 views
Skip to first unread message

Kiran

unread,
May 9, 2018, 3:58:20 AM5/9/18
to testng-users
Hi,

Question on retry logic for failed tests and following are the class files:

TestRetry class:
public class TestRetry implements IRetryAnalyzer {
int counter=0;
int retryLimit=4;

@Override
public boolean retry(ITestResult result) {
if (counter < retryLimit) {
  TestReporter.logStep("Retrying Test " +result.getName()+" for number of times: "+(counter+1));
  counter++;
          return true;
}
return false;
}

RetryListener class:
public class RetryListener implements IAnnotationTransformer {

@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// TODO Auto-generated method stub
IRetryAnalyzer retry = annotation.getRetryAnalyzer();

        if (retry == null) {

            annotation.setRetryAnalyzer(TestRetry.class);
}

}}

Test class:

@Listeners(RetryListener.class)
public class RetryLogicTest {
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) {
   for (ITestNGMethod method : context.getAllTestMethods()) {
          method.setRetryAnalyzer(new TestRetry());
   }
}

@Test(priority=0)
public void firsttest() {
TestReporter.assertEquals("Test", "Test", "pass");
}
@Test(priority=1, dependsOnMethods="firsttest")
public void secondtest() {
TestReporter.assertEquals("Test", "Test1", "fail");
}
@Test(priority=2,dependsOnMethods="secondtest")
public void thirdtest() {
TestReporter.assertEquals("Test", "Test", "pass");

}

when the above RetryLogicTest is run 

results are :
firsttest is passed
secondtest is failed and retried 5 times , on  5th time still it is failed
thirdtest is skipped because it depends on secontest

The above results are working as expected. 

Question
Since the secondtest depends on firsttest, thirdtest depends on secondtest. When one of them fails, I want to execute all the tests from first again. Is there a way to do it?

So any tests fail, I want all the test from first to be executed as it depends on each test.

Thanks!
Kiran



Reply all
Reply to author
Forward
0 new messages