Re: Retry Logic

19 views
Skip to first unread message

Kiran

unread,
May 8, 2018, 10:26:35 PM5/8/18
to Selenium Users
Hi,

Following are the classes used to implement retry logic

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

@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:
@Listeners(RetryListener.class)
public class SampleTest {

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

@Test(priority=0)
public void firsttest() {
System.out.println();
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 I execute the above test, following is the output
firsttest gets executed and passes
secondtest depends on firsttest and gets executed, its failed - Retried 3 times and failed again
thirdtest skipped because it depends on secondtest.

Output achieved as expected.

Question:
Since the tests are dependent. If one of the tests fails, I want to execute the whole class from first. is there a way to do it?

Examples: 
If secondtest fails, I want to execute the whole class SampleTest again.

Thanks!
Kiran

Krishnan Mahadevan

unread,
May 8, 2018, 10:53:19 PM5/8/18
to seleniu...@googlegroups.com

Kiran,

 

>>>>> Since the tests are dependent. If one of the tests fails, I want to execute the whole class from first. is there a way to do it?

No. TestNG doesn’t let you do something like that out of the box. You would need to build your own orchestration for getting this done, wherein you define only one @Test method, that would invoke other methods as an when required to complete a flow and then bind your @Test with a retry analyzer so that when the @Test fails, the entire orchestration gets called once again.

 

Something like below (I have posted the same on SO as an answer to your question : https://stackoverflow.com/a/50244425/679824 )

 

public class SampleTest {

 

    @BeforeSuite(alwaysRun = true)

    public void beforeSuite(ITestContext context) {

       for (ITestNGMethod method : context.getAllTestMethods()) {

           method.setRetryAnalyzer(new TestRetry());

       }

    }

 

    @Test (retryAnalyzer = TestRetry.class)

    public void orchestrateTest() {

        firsttest();

        secondtest();

        thirdtest();

    }

 

    public void firsttest() {

        System.out.println();

        TestReporter.assertEquals("Test", "Test", "pass");

    }

 

    public void secondtest() {

        TestReporter.assertEquals("Test", "Test1", "fail");

    }

 

    public void thirdtest() {

        TestReporter.assertEquals("Test", "Test", "pass");

    }

}

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
selenium-user...@googlegroups.com.
To post to this group, send email to
seleniu...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/selenium-users/cbbecb96-b079-48c3-a5c3-93ceda3187f5%40googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.

Joe Ward

unread,
May 9, 2018, 6:54:27 AM5/9/18
to seleniu...@googlegroups.com
 > Since the tests are dependent.

This is a pretty egregious antipattern, may I ask why you're doing this?

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to
selenium-users@googlegroups.com.


To view this discussion on the web visit

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4BF231D2-1499-4167-A4B4-35D21581F1EF%40gmail.com.
Reply all
Reply to author
Forward
0 new messages