Tony
Tony

Reputation: 1165

Inoperation of testng Retry Analyzer

I was following several different Web Sites explaining how to use RetryAnalyzer (they all say basically the same thing, but I checked several to see whether there was any difference). I implemented as they did in the samples, and deliberately caused a failure the first run (which ended up being the only run). Even though it failed, the test was not repeated. I even put a breakpoint inside the analyzer at the first line (res = false). which never got hit. I tell it to try 3 times but it did not retry at all. Am I missing something? My sample is below: Is it something to do with setting counter = 0? But the "res = false" at least should get hit?

public class RetryAnalyzer implements IRetryAnalyzer {

    int counter = 0;

    @Override
    public boolean retry(ITestResult result) {
        boolean res = false;
        if (!result.isSuccess() && counter < 3) {
            counter++;
            res = true;
        }
        return res;
    }
}

and

@Test(dataProvider = "dp",  retryAnalyzer = RetryAnalyzer.class)
public void testA(TestContext tContext) throws IOException {
    genericTest("A", "83701");
}

The test usually passes. I deliberately caused it to fail but it did not do a retry. Am I missing something?

=============================================== Default suite

Total tests run: 1, Failures: 1, Skips: 0

Upvotes: 0

Views: 736

Answers (2)

Ashish Tyagi
Ashish Tyagi

Reputation: 191

RetryAnalyzer class has to be public. Also, if it is an inner class, it should be static. TestNg silently ignores retryAnalyzer otherwise.

Upvotes: 0

Cosmin
Cosmin

Reputation: 2404

Try adding alwaysRun = true to your test method decorator.

@Test(dataProvider = "dp",  retryAnalyzer = RetryAnalyzer.class, alwaysRun = true)
public void testA(TestContext tContext) throws IOException {
    genericTest("A", "83701");
}

Also, before retrying, you may want to restart your driver instance, so that you start clean with your test. Otherwise, your second run will execute in the same browser instance.

Just do a driver.Quit() following by a reinstantiation of the browser driver.

Upvotes: 0

Related Questions