Search code examples
selenium-webdrivertestng

Testng - Update status of TesScript if it pass in second or third Re Run


We are re running failed tests using IRetryAnalyzer Class but if it got passed in second or third run the test case count is showing as 3 instead of one. I mean if it is passed in third attempt the total test case count showing as three instead of one.

If it reran 3 times and if it got passed in third time the result shown like below Total run:1 failed:0 skipped:0 in testng


Solution

  • This behavior is expected behavior. Each retry attempt is counted as a different test run.

    But if you want to update failed and skipped counts. You can try below snippet

    public class RetryAnalyzer implements IRetryAnalyzer {
        private int count = 0;
        private static final int MAX_RETRY_COUNT = 3;
        private int failedCount = 0;
        private int skippedCount = 0;
    
        @Override
        public boolean retry(ITestResult result) {
            if (count < MAX_RETRY_COUNT) {
                count++;
                if (result.getStatus() == ITestResult.FAILURE) {
                    failedCount++;
                } else if (result.getStatus() == ITestResult.SKIP) {
                    skippedCount++;
                }
                return true;
            }
            if (result.getStatus() == ITestResult.SUCCESS) {
                result.setStatus(ITestResult.SUCCESS);
            }
            return false;
        }
    
        public int getFailedCount() {
            return failedCount;
        }
    
        public int getSkippedCount() {
            return skippedCount;
        }
    }