Run Failed TestCases in TestNG

You may have more than 200 testcases on your regression test suite, sometime few testcases may fail. These failures may be caused by below reasons

1. Element not present
2. Element is not visible
3. Element not clickable
4. Browser slowness/responsivenes
There could be more reasons for failures

But we cannot run he total test suite again because we have failures, before coming to conclusion we have to be sure about the failures are not flaky failures.

Every time tests fail in a suite, TestNG creates a file called testng-failed.xml in the test-output directory. testng-failed.xml file contains the all the information to rerun only these methods that failed, allowing you to quickly reproduce the failures without having to run the entirety of your tests.


Program with failures

						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class TestNGClass 
						{
							WebDriver driver=new FirefoxDriver();
							@Test
							public void openSeleniumMentor()
							{
								driver.get("https://chercher.tech");
							}
							
							@Test
							public void openGoogle()
							{
								//fails
								driver.get("www.google.com");
							}
							@Test
							public void openGmail()
							{
								//fails
								driver.get("gmail.com");
							}
						}
					

Steps to Run Failed TestCases in TestNG

1.After execution on the "Result of Running Class" you see the details like PASSED and Failed, Skipped and time taken to execute.
testng options

2.Refresh the project and open the test-output folder
3.Now open the index, it might looks like below with pass and failed details
refresh testng options

4.Now you could see testng-failed.xml file in the test-output folder
test output

5.Now modify the program like below



						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class TestNGClass 
						{
							WebDriver driver=new FirefoxDriver();
							@Test
							public void openSeleniumMentor()
							{
								driver.get("https://chercher.tech");
							}
							
							@Test
							public void openGoogle()
							{
								//fails
								driver.get("http://www.google.com");
							}
							@Test
							public void openGmail()
							{
								//fails
								driver.get("gmail.com");
							}
						}
						


5.Now go to eclipse and right click on the testng-failed.xml
testng failed xml

6.Select Run As->TestNG Test
run testNg failed xml

7.Now it starts executing only those failed script and it skips the passed scenario. If you wonder why the look into the "testng-failed.xml"" it contains only the failed tests
out put of passed xml

8.Now check the "Result from Running Class"
out put of passed xml

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
 
Selenium-Webdriver.Com [Group]
Facebook Group · 801 members
Join Group
Learn Selenium Webdriver Concepts and Interview Questions Learn and code
 
Copyright © CherCher Tech