Remove (duplicate) failed TestNG result via test listener

后端 未结 3 1180
我在风中等你
我在风中等你 2021-01-13 06:50

Similar to the solution posted here TestNG retrying failed tests doesn't output the correct test results, I\'m trying to remove a (duplicate) test result using a test li

3条回答
  •  被撕碎了的回忆
    2021-01-13 07:21

    Try using this code:

    ListenerApadter:

    public class MyTestListenerAdapter extends TestListenerAdapter {
        @Override
        public void onTestFailure(ITestResult result) {
            if (result.getMethod().getRetryAnalyzer() != null) {
                MyRetryAnalyzer retryAnalyzer = (MyRetryAnalyzer)result.getMethod().getRetryAnalyzer();
    
                if(retryAnalyzer.isRetryAvailable()) {
                    result.setStatus(ITestResult.SKIP);
                } else {
                    result.setStatus(ITestResult.FAILURE);
                }
                Reporter.setCurrentTestResult(result);
            }
        }
    
       @Overrride
       public void onFinish(ITestContext context) {
         Iterator failedTestCases =context.getFailedTests().getAllResults().iterator();
        while (failedTestCases.hasNext()) {
            System.out.println("failedTestCases");
            ITestResult failedTestCase = failedTestCases.next();
            ITestNGMethod method = failedTestCase.getMethod();
            if (context.getFailedTests().getResults(method).size() > 1) {
                System.out.println("failed test case remove as dup:" + failedTestCase.getTestClass().toString());
                failedTestCases.remove();
            } else {
    
                if (context.getPassedTests().getResults(method).size() > 0) {
                    System.out.println("failed test case remove as pass retry:" + failedTestCase.getTestClass().toString());
                    failedTestCases.remove();
                }
            }
        }
       }
    }
    

    RetryAnalizer:

    public class MyRetryAnalyzer implements IRetryAnalyzer {
        private static int MAX_RETRY_COUNT = 3;
    
        AtomicInteger count = new AtomicInteger(MAX_RETRY_COUNT);
    
        public boolean isRetryAvailable() {
            return (count.intValue() > 0);
        }
    
        @Override
        public boolean retry(ITestResult result) {
            boolean retry = false;
            if (isRetryAvailable()) {
                System.out.println("Going to retry test case: " + result.getMethod() + ", " + (MAX_RETRY_COUNT - count.intValue() + 1) + " out of " + MAX_RETRY_COUNT);
                retry = true;
                count.decrementAndGet();
            }
            return retry;
        }
    }
    

    POM.xml -> Surefire Configuration:

    This is where you should configure "overwrite" surefire listener wich has their own counters.

    
      org.apache.maven.plugins
      maven-surefire-plugin
      2.18.1
      
        ${basedir}/testng.xml
      
       
        listener
        Utils.MyTestListenerAdapter,Utils.MyRetryAnalizer
       
     
    

提交回复
热议问题