1.

How to run only failed test cases?

Answer»

Many times test cases fail while running automated test scripts due to various reasons. It might be due to multiple reasons like network ISSUES, system issues or browser issues, etc. and we need to execute test scripts again using TestNG in Selenium. We can test these FAILED cases in two ways:

Case 1: Execute failed test cases using TestNG in Selenium – By using “testng-failed.xml”

  1. After the first run of the automated test run, right-click on Project - Click on Refresh
  2. A folder will be generated named the “test-output” folder, and inside “test-output” folder, you can find “testng-failed.xml”
  3. Run “testng-failed.xml” to execute the failed test cases again.

Case 2: Execute failed test cases using TestNG in Selenium – By Implementing TestNG IRetryAnalyzer.

Create a class to implement IRetryAnalyzer. Here we create a class (say, RetryFailedTestCases) and implementing IRetryAnalyzer.

package runfailedtestcases; import org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class RetryFailedTestCases implements IRetryAnalyzer {     private int retryCnt = 0;     //You can ntioned maxRetryCnt (Maximiun Retry Count) as PER your requirement. Here if any failed testcases then it runs two times     private int maxRetryCnt = 2;     //This method will be called every time a test fails and will return TRUE if a test fails and need to be retried, else it returns FALSE     public BOOLEAN retry(ITestResult result) {         if (retryCnt < maxRetryCnt) {             System.out.println("Retrying " + result.getName() + " again and the count is " + (retryCnt+1));             retryCnt++;             return true;         }         return false;     } }

A simple implementation of this ‘IAnnotationTransformer’ interface can help you set the ‘setRetryAnalyzer’ for ‘ITestAnnotation’. Add the above class name (RetryFailedTestCases.class) in the below program.

package runfailedtestcases; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.testng.IAnnotationTransformer; import org.testng.IRetryAnalyzer; import org.testng.annotations.ITestAnnotation; public class RetryListenerClass implements IAnnotationTransformer {  @Override  public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) {  IRetryAnalyzer retry = testannotation.getRetryAnalyzer();   if (retry == null) {  testannotation.setRetryAnalyzer(RetryFailedTestCases.class);  }  }

We can evaluate the above example by simple tests

Testcase 1:

package runfailedtestcases; import org.testng.Assert; import org.testng.annotations.Test; public class Test1 {  @Test  public void test1(){  System.out.println("Test 1");  Assert.assertTrue(true);  } }

Testcase 2:

package runfailedtestcases; import org.testng.Assert; import org.testng.annotations.Test; public class Test2 {  @Test  public void test2(){  System.out.println("Test 2");  Assert.assertTrue(false);  } }

As per the lines of code in Test2, it will fail. So Test2 will be executed 2 times as we took the maxRetryCnt as 2 in Retry Class. First, let us add the below-mentioned Listener to the testng.xml file. The below-mentioned syntax is to add Listener for RetryListnereClass subtext

<listeners>         <listener class-name="softwareTestingMaterial.RetryListenerClass"/> </listeners> Final testng.xml will look like below: <?xml VERSION="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="My Suite"> <listeners>         <listener class-name="softwareTestingMaterial.RetryListenerClass"/>   </listeners>  <test name="Test1">  <classes>  <class name="runfailedtestcases.Test1" />  </classes>  </test> <!-- Test -->  <test name="Test2">  <classes>  <class name="runfailedtestcases.Test2" />  </classes>  </test> <!-- Test --> </suite> <!-- Suite -->

After executing testng.xml the output shows Test 2 is executed 3 times as we have mentioned “maxRetryCnt=2”. Even though we have just 2 test cases, we could see total test runs are 4 in the result.

This way we could run failed test cases by applying TestNG in Selenium.



Discussion

No Comment Found