0

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

Tony
  • 1,127
  • 1
  • 18
  • 29

2 Answers2

0

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.

Cosmin
  • 2,354
  • 2
  • 22
  • 39
  • That did not work either. We do have an SQA guru who set testng up for us. I wonder whether he somehow replaces the retry analyzer with something so mine is getting overwritten. Is there any way for me to check that? – Tony Mar 22 '18 at 13:19
  • Check if your test class inherits from some other Base class, perhaps it was something there. Otherwise, some more details would be required. – Cosmin Mar 22 '18 at 13:34
  • It could also be an issue with the dataprovider. Check the post here: https://github.com/cbeust/testng/issues/877 – Cosmin Mar 22 '18 at 13:36
  • Hey Cosmin I notice you are Romanian. I am going to Cluj in 5 months for a wedding. Sorry to drift ;-) Yes the class does inherit. When I make retry anaylyzer static it is called, I see the counter increase. After counter reaches value, false is returned, but the test keeps executing and retrying. The counter stays at 3. I think it is the AbstractTest class that an employee here wrote. I guess I had better wait till he gets back from Canada and ask him for help? – Tony Mar 22 '18 at 14:05
0

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

Ashish Tyagi
  • 191
  • 3
  • 16