Executing Only Failed Tests in TestNG
Executing only failed tests in TestNG will discuss about how we can execute only failed test cases in the test suite once the execution of the suite completed. Most of the times when we execute bulk tests as suite then there is a chance of failing some of the tests.
For these failures there may be so many reasons. Will see some of them below:
1.Some of the elements may not load properly.
2.Due to some network slowness.
3.Functionality may got changed.
4.Locator value may got changed.. Etc…
Because of the reasons tests will fail. After completion of the execution if there are any failures, immediately we can not raise bugs. In the above scenarios failures due to 1,2 and 4 will not cause the bugs as this is because of loading elements and network slowness. But due to 3, we can raise the bugs.
But how we can come to know that the failures because of which reason. To get the confirmation we need to rerun the failed scripts to know the exact reason. Then how we can execute only failed tests?
To execute only failed tests we have provision in the TestNg. After completion of the suite execution; just refresh the project folder then you can find a folder called “test-output” and there you can find an xml file called “testng-failed.xml”. This xml file contains only failed test cases. So, we can execute this xml file to rerun the failed test again.
Below is the sample program:
import org.testng.Assert; import org.testng.annotations.Test; public class OnlyFailedTestExecution { @Test public void testOne() { Assert.assertTrue(true); System.out.println("Pass Test Case"); } @Test public void testTwo() { Assert.assertTrue(false); System.out.println("Faile Test Case"); } }
Execute the above program using below testng.xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="OnlyFailedTestExecution"/> </classes> </test> </suite>
Once your execute the above xml file then we can find the below output:
Now we want to execute only failed tests. For this we will go to the test-output folder and will execute the testng-failed.xml. Then we can see the below output.
In the above output we can see that only failed test got executed. And it ignores the execution of passed tests.
This way we can execute the only failed tests in testNg.
Please watch You tube video for better understanding.