Overview-

In this post, we will learn how to Implement retry analyzer in TestNg framework by overriding it.

In Test Automation, There are 2 types of failure-

1- Failure can occur, due to Script issue(false failure).

2- Actual application failure.

this post will cover-

1- How to Override retry analyzer listener

2- Different ways(#3) of executing it.

First of all, Extend iretryanalyzer in a class like below code, I have set counter values as “3”, this will rerun failed test cases where ever it has been implemented 3 times.

package Package_Automation;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer{
	int counter = 0;
	int retryLimit = 3;
	
	public boolean retry(ITestResult result) {
		// TODO Auto-generated method stub
		
		
		if(counter < retryLimit)
		{
			counter++;
			return true;
		}
		return false;			
	}
}

Lets understand class= “Class_withretryanalyzerImp “, this class has 2 methods.

1- TestOne-(in below code, retry analyzer is being called here, hardcoded Assert.fail() has been mentioned, which will fail this test, By Implementing it, this method will go for retry attempt 3 times

2- TestTwo- This method, is only mentioned for understanding purpose in reporting

package Package_Automation;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Class_withretryanalyzerImp {
	static int i =0;
	
	@Test(retryAnalyzer = Week2.RetryAnalyzer.class)
	 public void TestOne()
	 {		
		System.out.println("**Value of int retry attempt**"+i);
		i++;
		Assert.assertEquals(false, true);
	 }
	 
	 @Test
	 public void TestTwo()
	 {
	 Assert.assertEquals(false, true);
	 }
}

Now Implement this in your TestNg Tests in three different ways-

Method 1-

By calling before a specific @Test annotation, Please refer below code snippet, where reteryanalyzer is called before test method.

package Package_Automation;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Class_withretryanalyzerImp {
	
	static int i =0;
	
	@Test(retryAnalyzer = Week2.RetryAnalyzer.class)
	 public void TestOne()
	 {		
		System.out.println("**Value of int retry attempt**"+i);
		i++;
		Assert.assertEquals(false, true);
	 }
	 
	 @Test
	 public void TestTwo()
	 {
	 Assert.assertEquals(false, true);
	 }
}

Method 2

By Calling before Class, Refer below class where test class, in this case evey method inside this class will go for retry whenever any of the test methods is failed.

package Package_Automation;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test(retryAnalyzer = Week2.RetryAnalyzer.class)
public class Class_withretryanalyzerImp {
	
	static int i =0;
	
	
	 public void TestOne()
	 {		
		System.out.println("**Value of int retry attempt**"+i);
		i++;
		Assert.assertEquals(false, true);
	 }
	 
	 @Test
	 public void TestTwo()
	 {
	 Assert.assertEquals(false, true);
	 }
}

Method 3

By Calling retryanalyzer in XML file, will cover whichever test cases are inside testNG classes.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
        <listener class-name="Package_Automation.RetryAnalyzer"/>
  </listeners>
  <test  name="Test">
  <parameter name = "Browser" value="Chrome"/>
    <classes>
      <class name="Package_Automation.Class_withretryanalyzerImp"/>                   
    </classes>   
  </test> <!-- Test -->
</suite> <!-- Suite -->

TestNG test Output

Please refer below output, TestOne is mentioned three times, in total 4 times TestOne method is mentioned, first attempt without retry analyzer attempt, remaining 3 are attempting for retry

retryanalyzer Automatorsworlds
TestNg- result explained

Summary

Test cases failure is evident during test automation execution, therefore, retrying that particular failed test automatically is necessary so that seamless automation can be achieved.

Above Implementation can be achieved as per project need, Which means implementation at methods level, class level or XML level