无论如何在失败时重新运行测试类 [英] Is there anyway to rerun a test class when it fails

查看:25
本文介绍了无论如何在失败时重新运行测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当任何一个 @Test 失败时,我想重新运行一个测试类,包括它的 @BeforeMethod.我已经实现了 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.

推荐答案

这样做是可能的.为此,您需要在 testNg.xml 中将 org.testng.ITestListener 的实现注册为侦听器

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 必须实现 org.testng.ITestListener.

按如下方式实现 onTestFailure:

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.

另外,如果你想在不考虑测试结果的情况下只运行一个测试用例 n 次,那么你必须在 onTestFailure 方法中为计数器构建一个逻辑.

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.

-----------------------------更新----------------------------

发现了一个更优雅的解决方案实现 IRetryAnalyzer 接口.这个接口是由 TestNG 提供的,专门用于重试失败的测试.它提供了必须进行重试的次数.

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; 
     } 
 } 

您需要使用以下注释

 @Test(retryAnalyzer=Retry.class)

但是,为了避免将此属性添加到您的所有测试方法中采取以下方法,此链接引用'TestNG retryAnalyzer仅在方法@Test 中定义时有效,在类'@Test'

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());
      }
  }

这有望为您提供 testng-results.xml 报告.

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

这篇关于无论如何在失败时重新运行测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆