Retry Failed Test Cases in TestNG

Profile picture for user arilio666

TestNG is a testing framework followed by JUnit and NUnit but introduces some new functionalities which makes it more powerful and far easier to use. It is an open-source automated testing framework, where NG of TestNG means Next Generation. With the help of TestNG, we can achieve our testing requirements like functional, regression, sanity and end-to-end testing, and more.

This article will show how we can retry failed tests using TestNG.

1.) IRetryAnalyzer

This is an interface present to retry the failed test.

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.
 boolean retry(ITestResult result);

The method returns true if we want to retry failed test and returns false if we don't want to.
When blended with the test TestNG automatically invokes a retry analyzer to see if the test needs to retry in case it fails.

2.) Implementation

Let us create a class retry and implement IRetryAnalyzer.

package week4.day2;

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

public class Retry implements IRetryAnalyzer {

    int retryCount = 0;
    int maxRetryCount = 4;

    public boolean retry(ITestResult result) {

        if (!result.isSuccess()) {

            if (retryCount < maxRetryCount) {
                        "Retrying Test : Re-running " + result.getName() + " for " + (retryCount + 1) + " time(s).");

                retryCount++; // Increase the maxRetryCount by 1

                return true;
            } else {
        } else {


        return false;
  1. First, we check if the test failed with the if statement.
  2. We initiated the maxRetryCount.
  3. Then we check if the max number of test execution has been reached.
  4. We then print the number of retry attempts.
  5. We then increase the maxRetryCount by 1.
  6. Then mark the test as failed and rerun the failed test.
  7. TestNg marks the last test run as failed if the previous run is the max retry.
  8. In the else block, TestNG marks the test as passed when the test passes.
  9. So here, the test will run four times and then fails it.

3.) Example

To use the IRetryAnalyzer in our Test annotation follow this.

@Test(retryAnalyzer = Retry.class)
public void loginPage() {

Below we have an example of a Test Retryscenario.

package week4.day2;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class PopUp {
    ChromeDriver driver;

    private void setUp() throws InterruptedException {
        driver = new ChromeDriver();

    private void tearDown() {

    @Test(retryAnalyzer = Retry.class)
    public void popUp() throws Throwable {

        List<WebElement> elementTexts = driver.findElemens(By.xpath("//div[@class='header__main__left']"));

        for (WebElement webElement : elementTexts) {




  • We deliberately failed the test and can see that we achieved test retry here.
  • The method popUp has run four times as our maxRetryCount and achieved failure in the end.
  • So this is how we can retry failed tests in selenium java using TestNG.