How to Execute Failed Test Cases in TestNG

โšก Smart Summary

Running failed test cases in TestNG allows testers to isolate and re-execute only the tests that did not pass, saving time and reducing redundant execution across the full suite.

  • ๐Ÿ› ๏ธ Project Setup: Create a Java project in Eclipse, add TestNG and Selenium JAR libraries, and define two test classes (DemoA and DemoB).
  • ๐Ÿ”„ TestNG Conversion: Convert Java classes to a TestNG suite by right-clicking and selecting the TestNG convert option to generate testng.xml.
  • ๐Ÿ’ป Command Line Execution: Run the testng.xml suite from the command prompt using the java -cp command with correct classpath pointing to lib and bin folders.
  • โŒ Failed Test Detection: TestNG automatically generates a testng-failed.xml file inside the test-output folder whenever one or more test cases fail.
  • ๐Ÿ” Re-run Failed Tests: Execute testng-failed.xml either from Eclipse (Run As TestNG Suite) or the command line to re-run only the failed test cases.

Create a Selenium Project

Prepare the project before converting it to TestNG. The steps below walk through creating a Java project in Eclipse, setting up the package structure, and adding the required JAR libraries.

Step 1) In Eclipse, create a Java project by clicking on New Java Project.

Create a Selenium Project

Step 2) In this step:

  1. Give the project a name.
  2. Choose the execution environment.
  3. Select the project layout option.
  4. Click the Finish button.

Create a Selenium Project

After clicking Finish, the TestProject Java project is created and will appear in the Package Explorer.

Create a Selenium Project

Step 3) Open the newly created project. You will see the src folder in the Package Explorer.

  1. Right-click the project and select New.
  2. Select the Package option.

Create a Selenium Project

Step 4) In this step:

  1. Browse the src folder and select package.
  2. Give the package a name.
  3. Click Finish. (Package name: com.test)

Create a Selenium Project

After clicking Finish, the project structure will look like this:

Create a Selenium Project

Step 5) In this step:

  1. Right-click on the newly created package.
  2. Select the Class option.

Create a Selenium Project

A new window will open.

Step 6) In this step:

  1. Give the class a name.
  2. Select modifiers.
  3. Browse and select superclass java.lang.Object.
  4. Click Finish.

Here, you are creating two classes: DemoA and DemoB.

First, create class DemoA.

Create a Selenium Project

After clicking Finish, the class will be created:

Create a Selenium Project

Similarly, create class DemoB:

Create a Selenium Project

Step 7) If you have installed the TestNG library, click on Java Project โ†’ Properties.

Create a Selenium Project

Step 8) In the Properties window:

  1. Click on Java Build Path.
  2. Click on Libraries.
  3. Click on Add Library.

Create a Selenium Project

Next:

  1. Click on TestNG and then
  2. Click on Next.

Create a Selenium Project

Then click the Finish button.

Create a Selenium Project

After this, write the following code.

For Class DemoA:

Create a Selenium Project

Code Explanation:

  • driver.manage().window().maximize(); โ€” maximizes the browser window.
  • driver.get("https://www.google.co.in"); โ€” enters the specified URL in the address field.
  • driver.findElement(By.name("q")).sendKeys("Hi"); โ€” locates the Google search box and enters text using the sendKeys method.

Output: The above program contains errors, so it cannot be executed as-is.

Similarly, for Class DemoB:

Create a Selenium Project

The red underlined words indicate errors because the required JAR file has not yet been added. To fix these errors, add the corresponding JAR file. Right-click on the Java Project and select Properties.

Create a Selenium Project

Step 8 cont.) In the Properties window:

  1. Click on Java Build Path in the left navigation pane.
  2. Click on the Libraries tab.
  3. Click Add External JARs and select the Selenium standalone JAR file.
  4. Click OK.

Create a Selenium Project

After this step, all errors will be removed automatically. If any remain, hover over the error-marked code and import the necessary classes and interfaces. For @Test errors, hover and select Add TestNG Library.

Step 9) In this step:

  1. Right-click on the project and select the Folder option to create a folder named lib.
  2. Paste the TestNG JAR and Selenium JAR (selenium-server-standalone<version>) into this folder. Download both JARs from the web, copy them, then right-click the lib folder in Eclipse and select Paste.

Create a Selenium Project

Step 10) In this step:

  1. Select the parent folder.
  2. Give the folder the name lib. (This folder is required only for command-line execution; it is not needed when running testng.xml from Eclipse.)
  3. Click Finish.

Create a Selenium Project

After creating the folder, the next step is to convert the programs DemoA and DemoB into a testng.xml file.

Convert and Execute Selenium Project to TestNG

Step 1) In this step:

  1. Select the two Java files in the package and right-click.
  2. Select the TestNG option.
  3. Click Convert to TestNG.

Convert and Execute Selenium Project to TestNG

Step 2) A new window opens. Enter the following details:

  1. Location
  2. Suite Name
  3. Test Name
  4. Class Selection
  5. Parallel Mode
  6. Click Finish.

Convert and Execute Selenium Project to TestNG

The testng.xml file is created under the Java project and will look like this:

Convert and Execute Selenium Project to TestNG

(To run testng.xml from Eclipse: right-click the file in the Package Explorer and select Run As TestNG Suite.)

Step 3) The testng.xml suite file will look like this:

Convert and Execute Selenium Project to TestNG

If you want to execute DemoA first, remove its line and add it before DemoB:

Convert and Execute Selenium Project to TestNG

Convert and Execute Selenium Project to TestNG

Step 4) After executing the testng.xml file, the result will be displayed as follows. (No console output appears because no System.out.println() statements were written.)

Convert and Execute Selenium Project to TestNG

This is one way to execute tests through Eclipse. To run the same testng.xml suite from the command prompt, follow the steps below.

Execute TestNG via Command Line

To run a TestNG suite from the command line, you must first locate the project workspace path and construct the correct classpath command.

Step 1) Right-click on the Java project and select Properties. In the Properties window, select Resource.

Execute TestNG via Command Line

Step 2) Click the Resource link in the left navigation pane to see the exact project storage location.

Execute TestNG via Command Line

The project workspace is C:\Users\User\Desktop\Guru99\TestProject. Change the directory to that location in the command prompt.

Step 3) Type the following generic command:

java โ€“cp "path_of_lib_folder\*; path_of_bin_folder; path_of_testng.jar" org.testng.TestNG testng.xml

For this project, use:

Java โ€“cp "C:\Users\User\Desktop\Guru99\TestProject\lib\*;
  C:\Users\User\Desktop\Guru99\TestProject\bin" org.testng.TestNG testng.xml

Step 4) Press Enter. Both DemoA and DemoB will execute, and the result will be displayed in the command prompt.

Execute TestNG via Command Line

How to Run Only Failed Test Cases

If you want to execute only the failed test cases through Eclipse, first refresh the project.

Step 1) Right-click on the Java project (DemoA and DemoB) and select Refresh, or select the project and press F5.

Step 2) You will now see the test-output folder. Inside it, there is a file named testng-failed.xml.

Run Only Failed Test Cases

Step 3) Right-click on this file and select Run As โ†’ TestNG Suite.

Note: the test-output folder appears only when at least one test case has failed. If all test cases pass, the folder will not contain testng-failed.xml. Running this file re-executes only the failed test cases.

Run Only Failed Test Cases

Running testng-failed.xml from the Command Line

Step 1) Open the command prompt and navigate to the project workspace.

Running testng-failed.xml from Command Line

The project workspace is C:\Users\User\Desktop\Guru99\TestProject. Change directory to that location.

Step 2) Type the following generic command:

java โ€“cp "path_of_lib_folder\*; path_of_bin_folder; path_of_testng.jar" org.testng.TestNG test-output/testng-failed.xml

For this project:

Java โ€“cp "C:\Users\User\Desktop\Guru99\TestProject\lib\*;
C:\Users\User\Desktop\Guru99\TestProject\bin" org.testng.TestNG test-output/testng-failed.xml

Step 3) Press Enter. TestNG will run only the failed classes and display results for those classes.

In DemoB, if you change the code to the following, the element will not be found and an exception will be thrown. Because the exception is unhandled, the class will fail:

driver.findElement(By.name("a")).sendKeys("Bye");

Running testng-failed.xml from Command Line

FAQs

TestNG saves the failed test report as testng-failed.xml inside the test-output folder of your project after any test run that includes failures.

Yes. Right-click testng-failed.xml in Eclipse and select Run As TestNG Suite, or use the java -cp command in the command prompt pointing to test-output/testng-failed.xml.

TestNG only creates testng-failed.xml when at least one test fails. If all tests pass, the failed XML file is absent from test-output.

Yes. AI tools can analyze stack traces and testng-failed.xml content to pinpoint root causes, suggest code fixes, and recommend assertion improvements automatically.

AI can generate IRetryAnalyzer implementations, classify flaky vs. genuine failures, and auto-adjust retry counts based on historical test result patterns.

Summarize this post with: