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.
test-output/testng-failed.xmlGenerated testng-failed.xml example:
<?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:
mvn test -DsuiteXmlFile=test-output/testng-failed.xml
Option 3 — Programmatically re-run:
TestNG testNG = new TestNG();
testNG.setTestSuites(Arrays.asList("test-output/testng-failed.xml"));
testNG.run();
Option 4 — RetryAnalyzer (auto-retry on failure):
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:
- ✓Run full suite → some tests fail
- ✓TestNG generates
test-output/testng-failed.xml - ✓Re-run with
testng-failed.xml→ only failed tests execute - ✓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.
