Re: Retry Logic

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Retry Logic

Kiran-6
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Retry Logic

Krishnan Mahadevan

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/

From: <[hidden email]> on behalf of Kiran <[hidden email]>
Reply-To: <[hidden email]>
Date: Wednesday, May 9, 2018 at 7:56 AM
To: Selenium Users <[hidden email]>
Subject: [selenium-users] Re: Retry Logic

 

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

--
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
[hidden email].
To post to this group, send email to
[hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4BF231D2-1499-4167-A4B4-35D21581F1EF%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Retry Logic

Joe Ward
 > Since the tests are dependent.

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

On 9 May 2018 at 03:52, Krishnan Mahadevan <[hidden email]> wrote:

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/

From: <[hidden email]> on behalf of Kiran <[hidden email]>
Reply-To: <[hidden email]>
Date: Wednesday, May 9, 2018 at 7:56 AM
To: Selenium Users <[hidden email]>
Subject: [selenium-users] Re: Retry Logic

 

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

--
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
[hidden email].
To post to this group, send email to
[hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4BF231D2-1499-4167-A4B4-35D21581F1EF%40gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAJPyTs3aiQt57Xn%3D4LpaDiqGuF%2BVMpmou%2BZYu54PLkVaaKeHdQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.