How to Rerun Only Failed Test Cases in Selenium (Java + TestNG)

In large test suites, some test cases may occasionally fail due to network glitches, timing issues, or flakiness. Instead of rerunning the entire test suite, it’s more efficient to rerun only the failed test cases.

In this blog, we’ll explore a clean and easy way to do this using TestNG and Selenium WebDriver.


✅ Why Rerun Only Failed Tests?

  • Saves time in CI pipelines
  • Helps isolate flaky tests
  • Reduces test execution cost

🧪 Tools Required

  • Java
  • Selenium WebDriver
  • TestNG (with testng-failed.xml feature)

📝 Step-by-Step: Rerun Failed Test Cases in TestNG

🔹 Step 1: Create Your Test Classes

public class LoginTest {
  @Test
  public void testLogin() {
    WebDriver driver = new ChromeDriver();
    driver.get("https://example.com/login");
    // Test steps here
    driver.quit();
  }
}

🔹 Step 2: Run Your Suite (testng.xml)

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="MainTestSuite">
  <test name="LoginTests">
    <classes>
      <class name="tests.LoginTest" />
    </classes>
  </test>
</suite>

After execution, TestNG automatically generates a test-output/testng-failed.xml file containing only the failed test cases.


🔹 Step 3: Rerun the Failed Test Cases

Simply re-run this file:

$ testng test-output/testng-failed.xml

✅ This will only execute the failed tests, nothing else!


🔁 Advanced: Auto-Retry Using RetryAnalyzer

TestNG also allows you to automatically retry failed tests a few times before marking them as failed.

📄 Create Retry Class

public class RetryAnalyzer implements IRetryAnalyzer {
  private int count = 0;
  private static final int maxTry = 2;

  public boolean retry(ITestResult result) {
    if (count < maxTry) {
      count++;
      return true; // Retry
    }
    return false;
  }
}

🧷 Attach Retry to Your Test

@Test(retryAnalyzer = RetryAnalyzer.class)
public void testLogin() {
  // test logic here
}

This will automatically retry the test 2 times before marking it as failed.


💡 Best Practices

  • Use retry only for non-critical flaky tests
  • Always investigate recurring failures
  • Monitor retry trends in your reports

🏁 Conclusion

Rerunning only the failed test cases in Selenium + TestNG helps you:

  • Save time
  • Optimize CI pipelines
  • Improve stability of automation

Combine testng-failed.xml with RetryAnalyzer for robust test recovery strategy.

Leave a Comment

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

Scroll to Top