Retry Logic - retry whole class if one tests fails - selenium

杀马特。学长 韩版系。学妹 提交于 2019-12-24 07:15:48

问题


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);
    }

}}

SampleTest:

@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!


回答1:


There's currently no way of achieving what you are asking for.

TestNG will only retry a failed test, but will not go up the execution ladder to find out all the upstream dependencies and try running them as well (Your ask is a very specific variant of this generic use case).

If you come to think of it, a dependent test is being executed only because its upstream dependencies (methods on which it depends on) have been executed successfully. So if there's a failure in the current test, why would one need to re-execute the already satisfied upstream dependencies? Its counter intuitive.

For what you have as a use-case, you should be merely building the entire logic within a @Test method, wherein you take care of handling the retries and also the invocation of the entire chain once again, if there were failures.

The below sample should clarify that

public class SampleTest {

    @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");
    }
}

TestNG does not support the use case that you are looking for in your question.

On a side note, you cannot wire in a IAnnotationTransformer listener via an @Listeners annotation (this is explicitly called out in the javadocs of this interface). It can only be wired in via the <listeners> tag in your suite xml (or) by referring to it in the META-INF\services\org.testng.ITestNGListener file (its called the Service Provider Interface approach in Java)



来源:https://stackoverflow.com/questions/50241880/retry-logic-retry-whole-class-if-one-tests-fails-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!