2016-09-13 63 views
0

我添加了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); 
    } 

} 

}

但在測試報告中顯示或兩個失敗或一個過去了,一個失敗的測試

回答

1

奧爾加,我相信,您錯過了可以清理您的測試結果的附加監聽器:

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 - 必須單獨清理這些報告。

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

}

相關問題