Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to rerun a test class when it fails

Tags:

testng

I want to rerun a test class including its @BeforeMethod when any one of it's @Test fails. I have already implemented TestNG retry logic to rerun the failed test cases but I want to run entire class.

like image 731
Priyanshu Shekhar Avatar asked Sep 11 '14 07:09

Priyanshu Shekhar


2 Answers

It is possible to do so. For this you need to register an implementation of org.testng.ITestListener in testNg.xml as a listener

<listeners> 
    <listener class-name="com.xyar.OnTestFailureClass" />
</listeners

OnTestFailureClass must implement org.testng.ITestListener.

Implement onTestFailure as follows:

  public void onTestFailure(ITestResult result) {

      XmlSuite suite = new XmlSuite();
      suite.setName("rerunFailedTestClasses");
      XmlTest test = new XmlTest(suite);
      test.setName(result.getTestName());
      List<XmlClass> classes = new ArrayList<XmlClass>();
      classes.add(result.getTestClass().getXmlClass());
      test.setXmlClasses(classes) ;
      List<XmlSuite> suites = new ArrayList<XmlSuite>();
      suites.add(suite);
      TestNG tng = new TestNG();
      tng.setXmlSuites(suites);
      tng.run();

  }

CAUTION
You must have a good reason to rerun the test. Rerunning of the test must be desired when you know for sure that the second iteration will result in success. If this is not the case, then you will enter an infinite loop, wherein a failing tests will keep on getting executed and getting failed.

In addition, if you want to run a test case only n number of times irrespective of the test result, then you will have to build a logic for counter in the onTestFailure method.

-----------------------------UPDATE------------------------------------

Discovered a more elegant solution Implement IRetryAnalyzer interface. This interface has been provided by TestNG specifically for the purpose of retrying a failed test. It provided the number of times the retry must be done.

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzerImpl implements IRetryAnalyzer{
    private int retryCount = 0;
    private int maxRetryCount = 3;
    public boolean retry(ITestResult result) {
         if(retryCount < maxRetryCount) 
             { 
                retryCount++; 
                return true; 
             } 
         return false; 
     } 
 } 

you need use following annotations

 @Test(retryAnalyzer=Retry.class)

However, to avoid adding this attribute to your all test methods take the following approach which is referred by this link 'TestNG retryAnalyzer only works when defined in methods @Test, does not work in class' @Test'

  @BeforeSuite(alwaysRun = true)
  public void beforeSuite(ITestContext context) {
      for (ITestNGMethod method : context.getAllTestMethods()) {
          method.setRetryAnalyzer(new RetryAnalyzerImpl());
      }
  }

This should hopefully provide you with testng-results.xml report.

like image 89
DolphinJava Avatar answered Oct 05 '22 22:10

DolphinJava


Please, take a look: http://testng.org/doc/documentation-main.html#rerunning all failed tests are included to the separate xml suite, that can be rerun.

<suite name="allSuites">
  <suite-files>
    <suite-file path="yourSuite.xml" />
    <suite-file path="testng-failed.xml" />
    ...
  </suite-files>
</suite>
like image 32
German Petrov Avatar answered Oct 05 '22 21:10

German Petrov