Search code examples
javaseleniumtestngtestng-dataprovider

Unable to run the failed test cases using IRetryAnalyzer


I am having a test case that is purely data-driven. My objective is to rerun the tests which failed at least one more time again. The issue I am facing here is saying if the retry count is set to 3 and I have a data-driven test in the below fashion,

    @Test(dataProvider="PositiveScenarios",groups= "smoke",retryAnalyzer = utils.Retry.class)
    public void positiveScenariosTest(LinkedHashMap<String, String> data) throws InterruptedException {
    }

And the Retry class is like below

    package utils;

    import org.testng.IRetryAnalyzer;
    import org.testng.ITestResult;

    public class Retry  implements IRetryAnalyzer{

    int counter = 1;
    int retryMaxLimit = 3;

     public boolean retry(ITestResult result) {
    if (counter < retryMaxLimit) {
            System.out.println("Going to retry test case: " + result.getMethod() + ", " + (retryMaxLimit - counter + 1) + " out of " + retryMaxLimit);
            counter++;
            return true;
        }
        return false;
    }
}

Let's say I have 10 tests. If the 4th test fails, the 4th test retries 3 times ( which is good ). After this, say, if the 6th test fails, the 6th test runs only once and not 3 times. I was expecting the 6th tests to run 3 times before failing.

Not sure what I am doing wrong here.

I do not have any specific TestNG listeners here ( like on test failure ) Do I need to have one? If that's the case, how the first test failure ran 3 times? Is there an initializer I am missing.

Any help is greatly appreciated. I am using the latest version of TestNG.


Solution

  • I found out at last. Not sure whethere this is a TestNG issue. What I did was reset the counter each time when the test passes (even after 2nd round or 3 round ) like below

    @AfterMethod(alwaysRun = true)
        public void AfterTest(ITestResult result) throws IOException {
        if(result.getStatus() == ITestResult.FAILURE) {         
    
        }
        else if(result.getStatus() == ITestResult.SUCCESS) {
    
            Retry retry = new Retry();
            retry.resetCounter();
        }       
        else if(result.getStatus() == ITestResult.SKIP) {         
        }
    
        }
    

    Hope this will be useful for some one. Thanks again. It seems like a hack but working.