
๐ How to Execute Failed Test Cases in TestNG
Introduction
In TestNG, failed test cases can be re-executed without running the entire test suite again. This is useful for debugging and optimizing test execution.
This tutorial covers:
Creating a Selenium Project with TestNG
Executing Tests via Eclipse & Command Line
Running Only Failed Test Cases using testng-failed.xml
Automating Test Retries for Failed Tests
๐ Creating a Selenium TestNG Project
How to Execute Failed Test Cases in TestNG : To execute failed test cases, first, set up a Selenium TestNG project in Eclipse.
Step 1: Create a Java Project in Eclipse
Open Eclipse โ Click on File โ New โ Java Project
Enter Project Name (e.g., TestProject)
Choose Execution Environment & click Finish
๐ Project Structure After Creation:
bash
TestProject
โโโ src/
โโโ lib/ (Stores required JAR files)
โโโ test-output/ (Stores test reports)
โโโ testng.xml
๐ Step 2: Add Required JAR Files
1๏ธโฃ Right-click on the project โ Properties
2๏ธโฃ Click Java Build Path โ Libraries โ Add External JARs
3๏ธโฃ Add:
- Selenium Standalone JAR
- TestNG JAR
4๏ธโฃ Click OK
๐ Step 3: Create TestNG Test Classes
How to Execute Failed Test Cases in TestNG : Now, create two test classes (DemoA & DemoB) inside the package.
DemoA.java (Example Test Class)
java
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DemoA {
WebDriver driver;
@BeforeTest
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void googleSearch() {
driver.get(“https://www.google.com”);
driver.findElement(By.name(“q”)).sendKeys(“Hi”);
}
@AfterTest
public void teardown() {
driver.quit();
}
}
๐ This test opens Google and searches for โHi.โ
๐ DemoB.java (Failing Test Example)
java
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DemoB {
WebDriver driver;
@BeforeTest
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void invalidSearch() {
driver.get(“https://www.google.com”);
driver.findElement(By.name(“invalid”)).sendKeys(“Bye”); // Invalid locator (will fail)
}
@AfterTest
public void teardown() {
driver.quit();
}
}
๐ This test will fail because of an invalid element locator.
๐ Convert Selenium Project to TestNG
Step 1: Generate testng.xml
How to Execute Failed Test Cases in TestNG : Right-click on src folder โ TestNG โ Convert to TestNG
Enter:
- Suite Name: TestSuite
- Test Name: RegressionTest
- Class Selection: Select DemoA & DemoB
3๏ธโฃ Click Finish
Generated testng.xml:
xml
<suite name=”TestSuite”>
<test name=”RegressionTest”>
<classes>
<class name=”com.test.DemoA”/>
<class name=”com.test.DemoB”/>
</classes>
</test>
</suite>
๐ Running Tests & Identifying Failed Cases
Step 1: Run Tests in Eclipse
1๏ธโฃ Right-click testng.xml โ Run As โ TestNG Suite
2๏ธโฃ TestNG executes both DemoA and DemoB
3๏ธโฃ Since DemoB contains an invalid locator, it will fail
Step 2: Locate testng-failed.xml
How to Execute Failed Test Cases in TestNG : After test execution, failed test cases are recorded in testng-failed.xml inside the test-output/ folder.
๐ Re-Executing Only Failed Test Cases
Method 1: Run from Eclipse
Right-click testng-failed.xml โ Run As โ TestNG Suite
This will execute only the failed test cases (e.g., DemoB).
Method 2: Run from Command Line
How to Execute Failed Test Cases in TestNG : To execute failed tests via Command Line (CMD):
1๏ธโฃ Open Command Prompt
2๏ธโฃ Navigate to the project directory:
sh
cd C:\Users\User\Desktop\TestProject
3๏ธโฃ Run the following command:
sh
java -cp “lib/*;bin” org.testng.TestNG test-output/testng-failed.xml
โ This command runs only failed test cases stored in testng-failed.xml.
๐ Automating Test Retries Using IRetryAnalyzer
How to Execute Failed Test Cases in TestNG : Instead of manually re-running failed tests, TestNG provides an automatic retry mechanism.
Step 1: Create RetryAnalyzer.java
java
package com.listeners;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer {
private int retryCount = 0;
private static final int maxRetryCount = 2;
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
retryCount++;
return true;
}
return false;
}
}
This class retries failed tests up to 2 times.
Step 2: Apply Retry Logic to Test Cases
How to Execute Failed Test Cases in TestNG : Modify DemoB.java test case:
java
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.Listeners;
import org.testng.annotations.RetryAnalyzer;
@Listeners(com.listeners.ListenerTest.class) // Attach TestNG Listener
public class DemoB {
WebDriver driver;
@BeforeTest
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test(retryAnalyzer = com.listeners.RetryAnalyzer.class)
public void invalidSearch() {
driver.get(“https://www.google.com”);
driver.findElement(By.name(“invalid”)).sendKeys(“Bye”); // Invalid locator (will fail)
}
@AfterTest
public void teardown() {
driver.quit();
}
}
โ Now, if DemoB fails, it will retry automatically up to 2 times.
๐ Conclusion
๐น TestNG allows executing failed test cases separately using testng-failed.xml.
๐น Failed tests can be re-run via Eclipse or Command Line.
๐น Automating retries using IRetryAnalyzer helps improve test stability.
๐น TestNG enhances Selenium automation by enabling efficient test execution and logging.
Implement these techniques to optimize your TestNG Selenium tests!
TestNG Listeners in Selenium
Testing Selenium Download