Automation test fails can have many causes;
- Unstable tests
- The running environment is not ready
- Some framework problems
- Environment performance issues
- And so on and so forth…
This situation can often be frustrating and cause wasting time looking at errors constantly. Misleading situations can occur in automation results. When we add the retry mechanism in front of all these, we can pass our tests by making them more stable. TestNG provides a wonderful feature using which you can retry a test case multiple times before declaring it as Failed. What it means is that, if you see a failure can just automatically rerun the test to make sure that the test is consistently failing. This way it reduces false failures because of random issues and you spend more time debugging true failures.
In this chapter let us study how we can implement retry on failure mechanism in TestNG. In order to achieve this, we have to first understand the org.testng.IRetryAnalyzer interface. The interface definition is;
As you can see on the interface, it allows the test to retry when it fails according to the result of ITestResult with a single method. Let’s take a look at the variables maxRetryCount indicates how many times you need to retry. You can add a log to see what retry you are in debugging.
Let’s see how to add it to our RetryAnalyzer test.
Now, let’s see how we can actually use the retry analyzer in our tests. If you want to quickly see how it works, you can add the retry analyzer value in your @ Test(retryAnalyzer= IRetryAnalyzer Implementing class) like below:
Now let’s run our test and check how the retry analyzer works.
As you can see, since we set our retry count as 3, our test ran 3 times and it was retried after perpetrating the first two. After seeing the original perpetrator in the 3rd, since it reached maxRetryCount, retry returned false and our test failed.
If you want retryAnalyzer to work on all tests, there is more than one way to do it and I’ll show you one of them. We will process with Before Suite, but it is possible to perform the method I will show now with RetryListener.
You can add retryAnalyzer to all methods in BeforeSuite with setRetryAnalyzer method to your Base class as follows.
In this way, you don’t need to add anything next to @ Test annotations. Retry will run on all your tests.
As you can see, there are two tests, and although we did not define a retryAnalyzer next to our tests, it worked because we set a retryAnalyzer in Before Suite.
Do not forget to follow for more, next time I will share information about listeners in TestNG.