Answer
Running Failed Tests with testng-failed.xml
After every TestNG suite run, TestNG automatically generates test-output/testng-failed.xml containing only the tests that failed.
How It Works
- ✓Run your full suite — some tests fail
- ✓TestNG creates
test-output/testng-failed.xmlautomatically - ✓Run
testng-failed.xmlto re-execute ONLY those failures
testng-failed.xml Structure (Auto-Generated)
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Failed suite [Suite]" verbose="2">
<test name="Failed tests from Login Tests [LoginTest]">
<classes>
<class name="tests.LoginTest">
<methods>
<include name="invalidPasswordTest"/>
<include name="sessionTimeoutTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Run via Maven
Bash
# First run — some tests fail
mvn test
# Re-run only failures
mvn test -DsuiteXmlFile=test-output/testng-failed.xml
Run via IntelliJ / Eclipse
▸Right-click
test-output/testng-failed.xml → Run As → TestNG SuiteIRetryAnalyzer vs testng-failed.xml
| Approach | When | How |
|---|---|---|
IRetryAnalyzer | During current run | Automatically retries immediately |
testng-failed.xml | After run completes | Manual re-run of failures |
| Maven Surefire rerun | During run | <rerunFailingTestsCount>2</rerunFailingTestsCount> |
Maven Surefire — Auto-Retry on Failure
XML
<!-- pom.xml — automatically reruns failures up to 2 times -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
</configuration>
</plugin>
Jenkins Pipeline — Re-Run on Failure
GROOVY
pipeline {
agent any
stages {
stage('Run Tests') {
steps {
sh 'mvn test'
}
}
stage('Rerun Failures') {
when {
expression { currentBuild.result == 'FAILURE' }
}
steps {
sh 'mvn test -DsuiteXmlFile=test-output/testng-failed.xml'
}
}
}
}
Important Notes
- ✓
testng-failed.xmlis overwritten on every run — save a copy if needed - ✓It only includes
@Testmethods, not@Before*/@After*(those still run normally) - ✓If testng-failed.xml shows 0 tests = all passed
- ✓The file is in
test-output/by default — can configure output directory
