</>

Technology

TestNG

Difficulty

Intermediate

8 views

Interview Question

How to run failed test cases using TestNG in Selenium WebDriver?

TestNG automatically generates a testng-failed.xml file after a test run. Re-run only the failed tests by executing this XML file.

Answer

Running Failed Test Cases Using TestNG

After every TestNG test run, if any tests fail, TestNG automatically generates a testng-failed.xml file inside the test-output folder. This file contains only the failed tests.

Location: test-output/testng-failed.xml

Generated testng-failed.xml example:

XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Failed suite [MySuite]">
  <test name="Failed test [LoginTests]">
    <classes>
      <class name="tests.LoginTest">
        <methods>
          <include name="loginTest"/>
          <include name="dashboardTest"/>
        </methods>
      </class>
    </classes>
  </test>
</suite>

How to re-run failed tests:

Option 1 — Run testng-failed.xml directly: Right-click test-output/testng-failed.xml → Run As → TestNG Suite

Option 2 — Maven command:

Bash
mvn test -DsuiteXmlFile=test-output/testng-failed.xml

Option 3 — Programmatically re-run:

Java
TestNG testNG = new TestNG();
testNG.setTestSuites(Arrays.asList("test-output/testng-failed.xml"));
testNG.run();

Option 4 — RetryAnalyzer (auto-retry on failure):

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

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

    @Override
    public boolean retry(ITestResult result) {
        if (count < MAX_RETRY) {
            count++;
            return true;  // Retry the test
        }
        return false;
    }
}

// Apply to test:
@Test(retryAnalyzer = RetryAnalyzer.class)
public void flakeyTest() { }

Workflow:

  1. Run full suite → some tests fail
  2. TestNG generates test-output/testng-failed.xml
  3. Re-run with testng-failed.xml → only failed tests execute
  4. Continue until all pass

Key point: By using testng-failed.xml, you save time by not re-running passing tests — only the failures are re-executed.

Follow AutomateQA

Related Topics