Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

testng - Is there anyway to rerun a test class when it fails

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...