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.
package RetryAnalyzer;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetrySetup implements IRetryAnalyzer{
	
	// Variable to keep counter of retry
	private int retryCount = 0;
	
	// Variable to define max retry count
	private static final int maxRetryCount = 3;

	@Override
	public boolean retry(ITestResult result) {
		// We need to implement a cap, otherwise execution might go indefinite loop. 
		if(retryCount < maxRetryCount)
		{
			retryCount++;
			System.out.println("Retrying Test method : "+result.getName() + " for " + retryCount +" times. ");
			return true;
		}
		return false;
	}

}
2. Create an implemented class of IAnnotationTransformer interface and override transform method as :
package RetryAnalyzer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class MyAnnotationTransfer implements IAnnotationTransformer{

	@Override
	public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
		annotation.setRetryAnalyzer(RetrySetup.class);
		
	}

}

3. Create a TestNG class with some @Test method.
package RetryAnalyzer;

import org.testng.Assert;
import org.testng.annotations.Test;

public class RetryTest {
	
	private static int counter= 0;

	@Test
	public void method1()
	{
		counter++;
		if(counter <= 2)
		{
			System.out.println("Failing method purposefully.");
			Assert.fail("Purposefully failing");
		}
			
		System.out.println("Test passed");
	}

	
}

4. Add listener class name in testng.xml –




 	 	
 	 	
	
	



 


5. Run testng.xml as TestNG suite.

Output:

[RemoteTestNG] detected TestNG version 6.14.3
Failing method purposefully.
Retrying Test method : method1 for 1 times.
Failing method purposefully.
Retrying Test method : method1 for 2 times.
Test passed

===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 2
===============================================

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

Leave a Reply

Your email address will not be published. Required fields are marked *