通过重新运行测试删除(重复)失败的TestNG结果

时间:2016-09-13 17:36:45

标签: testing testng

我添加了Testng的重新运行测试,但在测试报告中遇到了重复测试的问题。你能帮帮我吗

private int retryCount = 0;
private int maxRetryCount = 1;

@Override
public boolean retry(ITestResult result) {
    if (retryCount < maxRetryCount) {
        System.out.println("Retrying test " + result.getName() + " with status "
                + getResultStatusName(result.getStatus()) + " for the " + (retryCount+1) + " time(s).");
        retryCount++;
        return true;
    }
    return false;
}

public String getResultStatusName(int status) {
    String resultName = null;
    if(status==1)
        resultName = "SUCCESS";
    if(status==2)
        resultName = "FAILURE";
    if(status==3)
        resultName = "SKIP";
    return resultName;
}

public class RetryListener implements IAnnotationTransformer {

@Override
public void transform(ITestAnnotation testannotation, Class testClass,
                      Constructor testConstructor, Method testMethod)   {
    IRetryAnalyzer retry = testannotation.getRetryAnalyzer();

    if (retry == null)  {
        testannotation.setRetryAnalyzer(iOpiumListener.class);
    }

}

}

但是在测试报告显示或两次失败或一次通过和一次测试失败

2 个答案:

答案 0 :(得分:1)

Olga,我相信你错过了额外的监听器来清理测试结果:

public class TestListener implements ITestListener {

    @Override
    public void onTestStart(ITestResult result) {
    }

    @Override
    public void onTestSuccess(ITestResult result) {
    }

    @Override
    public void onTestFailure(ITestResult result) {
    }

    @Override
    public void onTestSkipped(ITestResult result) {
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    }

    @Override
    public void onStart(ITestContext context) {
    }

    @Override
    public void onFinish(ITestContext context) {
        Set<ITestResult> failedTests = context.getFailedTests().getAllResults();
        for (ITestResult temp : failedTests) {
            ITestNGMethod method = temp.getMethod();
            if (context.getFailedTests().getResults(method).size() > 1) {
                failedTests.remove(temp);
            } else {
                if (context.getPassedTests().getResults(method).size() > 0) {
                    failedTests.remove(temp);
                }
            }
        }
    }
}

但是,请注意,它只适用于纯TestNG这一事实,如果您有其他依赖TestNG的工具,即Cucumber - 它会有必要单独清理这些报告。

答案 1 :(得分:0)

您可以捕获不正确的结果(onTestFailure)并在完成所有测试后删除它们(onFinish)

 
public class RetryAnalyzer implements IRetryAnalyzer {

   private static int retryCount = 0;
   private static final int maxRetryCount = 1; 
   private static boolean isRerun = false;

   @Override
   public boolean retry(ITestResult result) {

      if (retryCount < maxRetryCount) {

          retryCount++;
          isRerun = true;
          return true;
      } else {
          retryCount = 0;
          isRerun = false;
      }
      return false;
  }

  public boolean isRerun() {
      return isRerun;
  }

  private String getResultStatusName(int status) {
      String resultName = null;
      if (status == 1)
          resultName = "SUCCESS";
      if (status == 2)
          resultName = "FAILURE";
      if (status == 3)
          resultName = "SKIP";
      return resultName;
  }

}

public class TestListener implements ITestListener, IAnnotationTransformer {

private List<ITestResult> failedTestsToRemove = new ArrayList<>(); @Override public void onTestStart(ITestResult result) { } @Override public void onTestSuccess(ITestResult result) { } @Override public void onTestFailure(ITestResult result) { RetryAnalyzer retryAnalyzer = (RetryAnalyzer) result.getMethod().getRetryAnalyzer(); if (retryAnalyzer.isRerun()) { failedTestsToRemove.add(result); } } @Override public void onTestSkipped(ITestResult result) { } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { } @Override public void onStart(ITestContext context) { } @Override public void onFinish(ITestContext context) { for (ITestResult result : failedTestsToRemove) { context.getFailedTests().removeResult(result); } } @Override public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) { IRetryAnalyzer retryAnalyzer = testannotation.getRetryAnalyzer(); if (retryAnalyzer == null) { testannotation.setRetryAnalyzer(RetryAnalyzer.class); } }

}