How to correctly implement a RetryAnalyzer in TestNG

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.

Gaurav Kumar

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.

回复
Tony Chamberlain

SQA III, Multiplan

6 年

I just do something like assert (false) : fail";

回复
Tony Chamberlain

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 ===============================================

回复
Thilaknath Ashok kumar

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

回复
Erik Szalai

Software Engineer at T-Systems Switzerland

9 年

The pictures with the output are not readable :(

回复

要查看或添加评论,请登录

Sagar Shroff的更多文章

  • Testing LLM Query Outputs with Cosine Similarity

    Testing LLM Query Outputs with Cosine Similarity

    Introduction Few weeks ago, I was pondering on the thought on how to effectively test LLM based application features…

    5 条评论
  • TF-IDF Technique Overview

    TF-IDF Technique Overview

    I am currently learning about feature-engineering, and today I explored the TF-IDF technique. I decided to write it up…

    1 条评论
  • Java - Metaprogramming - Ability to add new functionality to existing API

    Java - Metaprogramming - Ability to add new functionality to existing API

    Ever came across scenario where you wished you could possibly add new functionality to an existing Java API? i.e say…

    2 条评论
  • Approaching test automation development

    Approaching test automation development

    In today's fast-paced product development, test-automation is one of the key to drive the organization's ability to…

    7 条评论
  • Attaching protractor to existing browser instance

    Attaching protractor to existing browser instance

    Protractor-test-runner by default behavior is Creates web-driver instance & opens up browser Executes your tests Kills…

  • Working along with Groovy Closures

    Working along with Groovy Closures

    Closures, they play a very key role in Groovy vocabulary. They are used everywhere in groovy API.

社区洞察

其他会员也浏览了