Table of contents
1.
Introduction
2.
Running Failed Test Cases in Selenium WebDriver
3.
Executing Failed Test Cases in TestNG
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Executing Failed Test Cases

Author Toohina Barua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

TestNG (Test Next Generation) is an open-source test automation framework based on JUnit and NUnit. Consider TestNG to be a more sophisticated version of the other two frameworks. It adds new features to the code, such as test annotations, grouping, prioritization, parameterization, and sequencing techniques, which were not previously available.

Not only does the TestNG framework manage test cases, but it also generates detailed reports on them. It includes a detailed summary that shows the number of failed test cases. The report also allows testers to precisely locate bugs and fix them as soon as possible.

In this article, we will be learning how to execute failed test cases using TestNG. So Let us dive in!

Running Failed Test Cases in Selenium WebDriver

Let's start by making a simple Java project and writing some test cases. Then, as shown below, create three class files.

Source

 

Also, make sure TestNG is configured in Eclipse. In this case, the tester has written simple test cases in each of the three 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");
}
}

 

Tests 1 and 2 will run smoothly and without errors. In Test3, the tester is attempting to cause the test case to fail on purpose in order to demonstrate how to run failed test cases. As a result, @Asert.assertTrue(false); is included in the code to fail this particular test case.

All of these test cases should be saved.

Now go to the project folder, right-click on the project, and select TestNG. Then choose Convert to TestNG from the menu. The same can be seen in the image below.

 

Source

 

This will create a testng.xml file for you, as shown below.

Source

 

Now click the testng.xml file, then run as, and then TestNG Suite. Take a look at the image below.

Source

 

Output:

Source

 

Executing Failed Test Cases in TestNG

Choose Refresh from the context menu when you right-click on the project. The tester will see a test-output folder when they refresh the page, as shown below.

Source

 

This test-output folder contains a variety of files, including failed test cases. See below.

 

Source

 

Suite is the name of the suite in the testng.xml file. As a result, there is a folder called Suite in the screenshot above. As shown below, expand that to get a file called testng-failed.xml.

Source

 

The following are the contents of the testng-failed:

<?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 states unequivocally that Test3 failed. The HTML representation of the Test is also available to the tester, as shown below:

 

Source

 

This clearly identifies which of the test cases failed. Return to Test3 and set the assert value to true before rerunning the testng-failed.xml file. Only the failed test case will be run, and the output will be correct. To double-check, run the original testng.xml file and see if all of the test cases are working properly.

Frequently Asked Questions

  1. Why use TestNG with Selenium?
    Default Selenium tests do not produce test results in a consistent format. We can generate test results in Selenium using TestNG.
     
  2. What type of reports does TestNG generate?
    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.
     
  3. How can we group multiple test cases?
    Multiple test cases can be grouped easily by converting them into a TestNG.xml file. Here, set the priorities to execute test cases.
     
  4. What is cross-browser testing?
    Using TestNG, one can execute multiple test cases on multiple browsers, i.e., cross-browser testing.
     
  5. With what tools can we integrate TestNG?
    The testing framework can be easily integrated with tools like Maven, Jenkins, etc.

Conclusion

Rerunning failed test cases with TestNG and Selenium WebDriver is simple, as explained in this article. Run the code, evaluate the results, and use Selenium and TestNG to streamline automated website testing.
We hope that this blog has helped you enhance your knowledge regarding Executing Failed Test cases and if you would like to learn more about TestNG, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass