TestNG Tutorials 69 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implementing With Listener – IAnnotationTransformer

Hello Folks,

We have already learnt about IRetryAnalyzer interface in previous post. We know why we use it and how can we use it.

From previous post, you know that we can pass implemented class of IRetryAnalyzer as an value to attribute named “retryAnalyzer” at @Test annotation. You need to add it each and every @Test annotation method if you want retry mechanism for each test method. It might be difficult if you have a large number of Test methods. If you want to remove the logic, you may need to remove it from each @Test method. 

So there is an another way of implementing the same if you want to add retry mechanism for each and every Test method. That way is using Listeners named IAnnotationTransformer. IAnnotationTransformer is a powerful listener provided by TestNG to give you a chance to modify a TestNG annotation read from your test classes. You can get or change the values you need by calling any of the getter/setters on the ITest interface.  This interface has only method named “transform”.

Method “transform has an parameter of type ITestAnnotation interface which has method to set (setRetryAnalyzer) and get (getRetryAnalyzer) RetryAnalyzer at run time. You need to pass your implemented class of IRetryAnalyzer interface as an argument to setRetryAnalyzer() method.

Step to add IRetryAnalyzer as a Listener:

  1. Create an implemented class of IRetryAnalyzer interface and override retry method as required.

2. Create an implemented class of IAnnotationTransformer interface and override transform method as :

3. Create a TestNG class with some @Test method.

4. Add listener class name in testng.xml –

5. Run testng.xml as TestNG suite.

Output:

You can notice the same behaviour as previous post. It is just another way of implementing retry mechanism.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning