2

I'm using the IRetryAnalyzer class in TestNG in order to relaunch tests that fail (up to 5 attempts). Right now if I run a test and it fails three times before passing, it says:

Tests run: 4, Failures: 3, Errors 0, Skipped 0.

What I want the results to show is:

Tests run: 1, Failures: 0, Errors 0, Skipped 0.

Basically, I don't want it to count as a failure unless it fails all 5 reruns. How do I do this?

4

2 回答 2

0

我刚刚做到了这一点,我是如何做到的,我认为上次成功之前的一切。我这样做是为了允许 3 次重新运行,这是我使用的代码:

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

public class RetryAnalyzer implements IRetryAnalyzer {
  private int count = 0;
  private int maxCount = 2;

  @Override
  public boolean retry(ITestResult result) {
    if (!result.isSuccess()) {
      if (count < maxCount) {
        count++;
        result.setStatus(ITestResult.SUCCESS);
        String message = Thread.currentThread().getName() + ": Error in " + result.getName() + " Retrying "
            + (maxCount + 1 - count) + " more times";
        System.out.println(message);
        Reporter.log(message);
        return true;
      } else {
        result.setStatus(ITestResult.FAILURE);
      }
    }
    return false;
  }
}

这里的关键是当我还在重试时,我将结果设置为成功

result.setStatus(ITestResult.SUCCESS)

当尝试已经完成但仍然失败时,将其设置为失败

result.setStatus(ITestResult.FAILURE);
于 2013-07-23T22:26:37.773 回答
0

由于没有人回复,我试一试。

只是我在阅读您的帖子时产生的随机想法,而我正在寻找解决问题的方法...

@AfterTest你可以通过你的方法得到你的测试结果ITestResult result

@AfterMethod
public void AfterMethod_CheckForFailure(ITestResult result, Method m)

@AfterMethod当带有@Test注解的方法完成时调用an 。能够从static具有成员的类中读取当前迭代static,如果它<5然后使用

result.setStatus(ITestResult.SUCCESS); // or what ever status you want
setCurrentTestResult(result);

不知道这是否有效,但在我看来它可以:) 祝你好运

于 2012-08-28T17:07:08.673 回答