---
description: Previously you were executing testng.xml file through eclipse only. Here, you will execute the same testng.xml file using the command line and storing logging information using Reporter class present
title: How to Execute Failed Test Cases in TestNG
image: https://www.guru99.com/images/how-to-execute-failed-test-cases-in-testng.png
---

[Skip to content](#main)

**⚡ 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.

Read More

![](https://www.guru99.com/images/how-to-execute-failed-test-cases-in-testng.png)

## 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](https://www.guru99.com/java-tutorial.html) project by clicking on New Java Project.

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor3.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor4.png)

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

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor5.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor6.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor7.png)

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

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor8.png)

**Step 5)** In this step:

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

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor9.png)

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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor10.png)

After clicking **Finish**, the class will be created:

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor11.png)

Similarly, create class DemoB:

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor14.png)

**Step 7)** If you have installed the TestNG library, click on **Java Project → Properties**.

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor17.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor18.png)

Next:

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

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor19.png)

Then click the **Finish** button.

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor20.png)

After this, write the following code.

**For Class DemoA:**

[![Create a Selenium Project]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor21.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor22.png)

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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor23.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor24.png)

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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor25.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor26.png)

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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor27.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor29.png)

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

[![Convert and Execute Selenium Project to TestNG]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor30.png)

(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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor31.png)

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

[![Convert and Execute Selenium Project to TestNG]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor32.png)

[![Convert and Execute Selenium Project to TestNG]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor33.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor34.png)

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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor35.png)

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

[![Execute TestNG via Command Line]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor36.png)

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

**Don't Miss:**

- [WebElement in Selenium](https://www.guru99.com/accessing-forms-in-webdriver.html)
- [Implicit and Explicit Wait in Selenium with Syntax](https://www.guru99.com/implicit-explicit-waits-selenium.html)
- [How to Upload & Download a File using Selenium](https://www.guru99.com/upload-download-file-selenium-webdriver.html)
- [Refresh Page using Selenium Webdriver](https://www.guru99.com/selenium-refresh-page.html)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor38.png)

### 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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor39.png)

**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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor40.png)

### 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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor41.png)

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]()](https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor42.png)

## FAQs

📂 Where does TestNG save the failed test report?

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.

🔄 Can testng-failed.xml be run from both Eclipse and the command line?

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.

❓ Why does the test-output folder not appear after a successful run?

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.

🤖 Can AI tools help identify why a TestNG test failed?

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

🧠 How can AI assist in building smarter TestNG retry strategies?

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:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-execute-failed-test-cases-in-testng.png","url":"https://www.guru99.com/images/how-to-execute-failed-test-cases-in-testng.png","width":"700","height":"250","caption":"How to Execute Failed Test Cases in TestNG","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/selenium","name":"Selenium"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html","name":"How to Execute Failed Test Cases in TestNG"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html#webpage","url":"https://www.guru99.com/run-failed-test-cases-in-testng.html","name":"How to Execute Failed Test Cases in TestNG","dateModified":"2026-06-19T17:55:29+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-execute-failed-test-cases-in-testng.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta","url":"https://www.guru99.com/author/admin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","url":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","caption":"Krishna Rungta","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Selenium","headline":"How to Execute Failed Test Cases in TestNG","description":"Previously you were executing testng.xml file through eclipse only. Here, you will execute the same testng.xml file using the command line and storing logging information using Reporter class present","keywords":"selenium","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta"},"dateModified":"2026-06-19T17:55:29+05:30","image":{"@id":"https://www.guru99.com/images/how-to-execute-failed-test-cases-in-testng.png"},"copyrightYear":"2026","name":"How to Execute Failed Test Cases in TestNG","subjectOf":[{"@type":"HowTo","name":"How to run only failed test cases","description":"If you want to execute only failed test cases through the Eclipse, then first refresh the project.","step":[{"@type":"HowToStep","name":"Step 1) Right click on the java project (Demo A and B).","text":"Select the refresh option or simply select the java project and press F5.","url":"https://www.guru99.com/run-failed-test-cases-in-testng.html#step1"},{"@type":"HowToStep","name":"Step 2) Then you will able to see the test-output folder.","text":"In that folder, you have a file with name testng-failed.xml.","image":"https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor39.png","url":"https://www.guru99.com/run-failed-test-cases-in-testng.html#step2"},{"@type":"HowToStep","name":"Step 3) Right-click on this file and click on run as and select the option called 'testNG suite'.","text":"Suppose if you have three test cases if all the test cases are executed successfully means you are not able to see this folder under the test-output folder.","image":"https://www.guru99.com/images/8-2016/090116_1045_TestNGTutor40.png","url":"https://www.guru99.com/run-failed-test-cases-in-testng.html#step3"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Where does TestNG save the failed test report?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can testng-failed.xml be run from both Eclipse and the command line?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Why does the test-output folder not appear after a successful run?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can AI tools help identify why a TestNG test failed?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI tools can analyze stack traces and testng-failed.xml content to pinpoint root causes, suggest code fixes, and recommend assertion improvements automatically."}},{"@type":"Question","name":"How can AI assist in building smarter TestNG retry strategies?","acceptedAnswer":{"@type":"Answer","text":"AI can generate IRetryAnalyzer implementations, classify flaky vs. genuine failures, and auto-adjust retry counts based on historical test result patterns."}}]}],"@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html#schema-205788","isPartOf":{"@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/run-failed-test-cases-in-testng.html#webpage"}}]}
```
