---Advertisement---

How to Execute Failed Test Cases in TestNG: Selenium WebDriver Best 2025

By Manisha

Updated On:

---Advertisement---
How to Execute Failed Test Cases in TestNG

๐Ÿ‘‰ How to Execute Failed Test Cases in TestNG

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.

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.

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

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

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

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

Right-click testng-failed.xml โ†’ Run As โ†’ TestNG Suite
This will execute only the failed test cases (e.g., DemoB).


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.

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.


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 

Leave a Comment

Index