App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide How to run failed test cases using TestNG in Selenium Webdriver?

How to run failed test cases using TestNG in Selenium Webdriver?

By Neha Vaidya, Community Contributor -

In every software testing process, there are inevitably times when test cases fail.

Test cases usually fail due to server and network issues, an unresponsive application or validation failure, or even due to scripting issues. When failures occur, it is necessary to handle test case management and rerun them to get the desired output. An efficient way to do this is to use the TestNG suite.

This article will delve into how to run failed test cases in TestNG using Selenium WebDriver.

Introduction to TestNG

TestNG stands for Test Next Generation and it is an open-source test automation framework inspired by JUnit and NUnit. Think of TestNG as an upgraded version of the other two frameworks. It provides additional functionalities such as test annotations, grouping, prioritization, parameterization, and sequencing techniques in the code – features that were not provided earlier.

TestNG framework not only manages test cases but also provides detailed reports of those tests. It provides a detailed summary that displays the number of test cases that have failed. The report also enables testers to accurately locate bugs and fix them at the earliest.

Also Read: How to Automate TestNG using Selenium

Why TestNG?

TestNG offers the following features:

  • It generates the report in a proper format that includes the number of test cases executed, the number of failed test cases, and the test cases that have been skipped.
  • Multiple test cases can be grouped easily by converting them into TestNG.xml file. Here, set the priorities to execute test cases.
  • Using TestNG, one can execute multiple test cases on multiple browsers, i.e., cross browser testing.
  • The testing framework can be easily integrated with tools like Maven, Jenkins, etc.

testng

Before proceeding with this article, have a look at this TestNG tutorial which discusses how to execute test cases using this framework.

How to Run a Failed Test Case using TestNG in Selenium WebDriver?

First, let’s create a simple java project and write test cases. Next, create 3 class files as shown below.

How to run failed test cases in testng

Also, make sure to configure TestNG into Eclipse. The tester, in this example, has written simple test cases in all the 3 class files as shown below.

Test1.java

package testngtest;
import org.testng.annotations.Test;
@Test
public class Test1 {
public void testwhatsapp() {
@Asert.assertTrue(true);
System.out.println("Whatsapp is working fine");
}
}

Test2.java

package testngtest;
import org.testng.annotations.Test;
@Test
public class Test1 {
public void testlinkedin() {
@Asert.assertTrue(true);
System.out.println("Linkedin is working fine");
}
}

Test3.java

package testngtest;
import org.testng.annotations.Test;
@Test
public class Test1 {
public void testgoogle() {
@Asert.assertTrue(false);
System.out.println("Google is working fine");
}
}

Test1 and Test2 will work fine without any errors. In case of Test3, the tester is purposely trying to cause the test case to fail in order to showcase how to run failed test cases. Therefore, the code includes @Asert.assertTrue(false); to fail this particular test case.

Save all these test cases.

Now, go to the project folder > right click on the project > Choose TestNG > Convert to TestNG. The snapshot below depicts the same.

How to rerun failed test cases in testng

This will automatically generate a testng.xml file as shown below.

Run failed test cases in testng

Now, click on the testng.xml file and run as > TestNG Suite. Refer to the snapshot below.

Testng execution

Output goes as follows:

How to execute failed test cases in testng

As shown, there is one failure in the total run. It is easy to go back and check the class files manually to understand the error as there are only 3 test cases.

But, when there are thousands of test cases, checking class files manually and then resolving the errors is time-consuming and tedious. Hence, it is necessary to use TestNG to identify failed test cases and re-execute them.

Run Selenium Tests for Free

How to Run Failed Test Cases in TestNG?

Right click on the project and choose Refresh. On refreshing, the tester will see a test-output folder as shown below.


This test-output folder comprises various files that include failed test cases as well. See below.

In the testng.xml file, the suite name is Suite. Therefore, in the above snapshot, there is a folder called Suite. Expand that to get a file called testng-failed.xml as shown below.

Contents of the testng-failed will be as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="Failed suite [Suite]">
<test thread-count="5" name="Test(failed)">
<classes>
<class name="testngtest.Test3">
<methods>
<include name="testgoogle"/>
</methods>
</class> <!-- testngtest.Test3 →
</classes>
</test> <!-- Test(failed) →
</suite> <!-- Failed suite [Suite] -->

It clearly mentions that Test3 has failed. The tester can also view the HTML representation of the Test as shown below:

How to execute failed test cases in testng example

This clearly shows which test case failed.

Let us next see how to rerun failed test cases in TestNG. Go back to Test3, change the assert value to true and rerun the testng-failed.xml file. This will execute only the failed test case with the correct output. In order to cross-check, execute the original testng.xml file and check if all the test cases are working as expected.

As this article explains, it requires only a few steps to rerun failed test cases with TestNG and Selenium WebDriver. Run the code, evaluate the results, and start streamlining automated website tests with Selenium and TestNG.

Needless to say, these tests are always best run on real browsers and devices. BrowserStack offers a cloud Selenium Grid of 3000+ real browsers and devices, which testers can access to run Selenium tests. Simply sign up, choose the browser-device-OS combination required, and start testing for free.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to Install and Add TestNG in Eclipse

How to run parallel test cases in TestNG

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack