Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 68 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implement Using retryAnalyzer attribute in the @Test annotation

Posted on 03/16/2025 By admin

Hello Folks,

TestNG provides a built in mechanism to re-run failed test case for N times (Ideally) instantly using IRetryAnalyzer interface. It means that if a test method fails, we can rerun it for N times or until it is passed during run. Let’s see this concept in more details:

IRetryAnalyzer Interface:

IRetryAnalyzer is an interface in org.testng package. It has only method named retry. By overriding this method in your class, you can control the number of attempts to rerun a failed test case.  You can refer code of IRetryAnalyzer below:

package org.testng;

/**
 * Interface to implement to be able to have a chance to retry a failed test.
 *
 * @author [email protected] (Jeremie Lenfant-Engelmann)
 *
 */
public interface IRetryAnalyzer {

  /**
   * Returns true if the test method has to be retried, false otherwise.
   *
   * @param result The result of the test method that just ran.
   * @return true if the test method has to be retried, false otherwise.
   */
  public boolean retry(ITestResult result);
}

When a @Test annotated method has value for “retryAnalyzer” attribute, reTry method will be called only if Test method is failed. “retry” method will not be executed at all if Test method is passed or skipped.

How to implement above mechanism?

Since IRetryAnalyzer is an interface, we must have to implement it in our class to provide definition to “retry” method. You can implement this interface in any normal class or TestNG class. If you keep a separate implemented class, it will be good for best readability and maintain. So, let’s create a class and implement  IRetryAnalyzer interface.

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;
        }

}

Now, you need to add an attribute called “retryAnalyzer” to @Test annotated method with value as Implemented class name. If we do not add this attribute, remember “retry” will not be called automatically. It provides you flexibility of trying to rerun those test method which fails sometimes because of some random issues (not functional) only.

package RetryAnalyzer;

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

public class RetryTest {

        // Need to mention implemented class name of IRetryAnalyzer
        @Test(retryAnalyzer= RetrySetup.class)
        public void method1()
        {
                System.out.println("Failing method purposefully.");
                Assert.fail("Purposefully failing");  
        }

        
}

Execution flow:

  1. “method1” will be run and it will fail as we are failing it wantedly.
  2. Then control will go to retry method of “RetrySetup class as @Test annotation has “retryAnalyzer attribute.
  3. Since retryCount < maxRetryCount (0 < 3),  retry will return true and retryCount counter will be increased by one. “method1” will be executed again. Earlier run will be marked as skipped.
  4. Same flow will be repeated till ( retryCount < maxRetryCount) becomes false and finally “method1” will be marked as failed.

Output:

[java] [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. Failing method purposefully. Retrying Test method : method1 for 3 times. Failing method purposefully. FAILED: method1 java.lang.AssertionError: Purposefully failing at org.testng.Assert.fail(Assert.java:96) at RetryAnalyzer.RetryTest.method1(RetryTest.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:583) at org.testng.internal.Invoker.retryFailed(Invoker.java:839) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1010) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)

at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Uncategorized

Post navigation

Previous Post: Selenium Interview Question 4 – Difference Between @FindBy and findElement() Method in Selenium WebDriver
Next Post: How To Install Java 8 On Windows 10

Related Posts

image – Make Selenium Easy Uncategorized
Frequently Asked Java Programs In Interview Archives – Page 2 of 3 – Make Selenium Easy Uncategorized
How to Verify if Options in Dropdown are Sorted as Expected In Selenium WebDriver – Java | Make Selenium Easy Uncategorized
Amod Mahajan, Author at Make Selenium Easy – Page 47 of 47 Uncategorized
CombiningedXpath – Make Selenium Easy Uncategorized
TestNG Tutorials 50: DataProvider in TestNG – Understand Basics of DataProvider & How It Works | Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark