</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

How do you run only failed test cases using testng-failed.xml in TestNG?

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

  1. Run your full suite — some tests fail
  2. TestNG creates test-output/testng-failed.xml automatically
  3. Run testng-failed.xml to 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 Suite

IRetryAnalyzer vs testng-failed.xml

ApproachWhenHow
IRetryAnalyzerDuring current runAutomatically retries immediately
testng-failed.xmlAfter run completesManual re-run of failures
Maven Surefire rerunDuring 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.xml is overwritten on every run — save a copy if needed
  • It only includes @Test methods, 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

Follow AutomateQA

Related Topics