How to correctly implement a RetryAnalyzer in TestNG
Sagar Shroff
Sr Software Development Engineer In Test - Selenium | Cucumber | Karate | Cypress | Javascript | Java | AWS
A very common requirement while automating testing of a product is you may wish to implement a retry mechanism for a particular case, may be because of setup and network problem. TestNG provides an interface for the same.
Below is my IRetryAnalyzer implementation
In above code, MaxRetryCount annotation is used to configure max no of retry for a failed case.
We will use the above annotation to configure MaxRetry.
In order for TestNG to know about retry analyzer implementation, it provides a retryAnalyzer property in @Test annotation. So below is a small snippet of how the test configuration would look like
Above configures max retries to 3 and retryAnalyzer to the implementation.
For each test method wherever retry logic is needed, retryAnalyzer property set will be duplicated. To avoid this we can use IAnnotationTransformer to automatically configure retryAnalyzer for us. TestNG provides this interface to facilitate dynamic configuration during initialization of test methods.
For each of your test methods, TestNG will invoke IAnnotationTransformer.transform.
We need to register the above implementation as a listener in testng.xml. With that in place, we no longer need to duplicate retryAnalyzer config in our test methods as shown below.
Cheers, Do let me know if you have any doubts.
Lead Consultant SDET at AllState India
1 年Hi Sagar. You solution will work well if one test have one data set. Suppose we have multiple data set tied to one test case then how to handle that one. Like one test have 2 data set. first data set passed for that test but 2nd data set failed for same test then retry logic again try to rerun the first data set only from start even tough first dataset had passed for that test case. How to handle that? Retry to rerun the only failed data set in one test case if we have more than one data set tied to one test case.
SQA III, Multiplan
6 年I just do something like assert (false) : fail";
SQA III, Multiplan
7 年I could not get this to work for me. I did what you did (WIthout the @maxRetryCount because it did not recognize the annotation): 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 ===============================================
Senior DevOps Engineer
9 年Hi Sagar, Adding this logic increases the Tests Run (Lets say i am running one test case) and have a re run threshold of 1. When the final output is printed it mentions Tests run as 2 instead of 1, is there a way we can limit it to 1
Software Engineer at T-Systems Switzerland
9 年The pictures with the output are not readable :(