TestNG retrying failed tests doesn't output the correct test results

后端 未结 5 1189
别那么骄傲
别那么骄傲 2020-12-13 07:41

Setup: I have a class that extends the IRetryAnalyzer and have implemented a simple retry logic overriding the following method: public boolean retry(IT

5条回答
  •  天涯浪人
    2020-12-13 08:09

    My approach builds on Morvader's answer but adds the ability to define retry analyzers that adhere to the original intent of actually failing the test even if the method have passed after some retries.

    I also didn't find a need to tend to the number of test in the onFinish() method, the numbers seemed fine in maven-surefire-plugin version 2.18

    RetryListenerAdapter

    public class RetryListenerAdapter extends TestListenerAdapter {
    
        @Override
        public void onTestFailure(ITestResult tr) {
            IRetryAnalyzer retryAnalyzer = tr.getMethod().getRetryAnalyzer();
            if (retryAnalyzer == null || !(retryAnalyzer instanceof IRetryAnalyzerWithSkip)) {
                super.onTestFailure(tr);
            } else if (((IRetryAnalyzerWithSkip) retryAnalyzer).isRetryable()) {
                tr.setStatus(ITestResult.SKIP);
                super.onTestSkipped(tr);
            } else {
                super.onTestFailure(tr);
            }
        }
    }
    

    IRetryAnalyzerWithSkip

    public interface IRetryAnalyzerWithSkip extends IRetryAnalyzer {
        boolean isRetryable();
    }
    

    Retry

    public class Retry implements IRetryAnalyzerWithSkip {
        private int retryCount = 0;
        private int maxRetryCount = 3;
    
        public boolean retry(ITestResult result) {
    
            if (retryCount < maxRetryCount) {
                retryCount++;
                return true;
            }
            return false;
        }
    
        @Override
        public boolean isRetryable() {
            return retryCount < maxRetryCount;
        }
    }
    

提交回复
热议问题