Created
April 11, 2024 05:26
-
-
Save SarahElson/95a9d0510c17b44ffe1c7af3d2a73bdd to your computer and use it in GitHub Desktop.
How To Retry Failed Tests Using IRetryAnalyzer In TestNG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package io.github.mfaisalkhatri.listeners; | |
| import org.apache.logging.log4j.LogManager; | |
| import org.apache.logging.log4j.Logger; | |
| import org.testng.IRetryAnalyzer; | |
| import org.testng.ITestResult; | |
| public class Retry implements IRetryAnalyzer { | |
| private static final Logger LOG = LogManager.getLogger ("Retry.class"); | |
| private static final int maxTry = 3; | |
| private int count = 0; | |
| @Override | |
| public boolean retry (final ITestResult iTestResult) { | |
| if (!iTestResult.isSuccess ()) { | |
| if (this.count < maxTry) { | |
| LOG.info ("Retrying test " + iTestResult.getName () + " with status " + getResultStatusName ( | |
| iTestResult.getStatus ()) + " for the " + (this.count + 1) + " time(s)."); | |
| this.count++; | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public String getResultStatusName (final int status) { | |
| String resultName = null; | |
| if (status == 1) { | |
| resultName = "SUCCESS"; | |
| } | |
| if (status == 2) { | |
| resultName = "FAILURE"; | |
| } | |
| if (status == 3) { | |
| resultName = "SKIP"; | |
| } | |
| return resultName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment