Skip to main content
  1. Articles/

How to Rerun Only Failed Testcases using TestNG

·3 mins

debug the failed

  • Now that you guys know how to execute the TestNG class and TestNG suite using testng.xml (if not, here is my Medium page), you would have also encountered failure of some testcases.

  • The reason for the failures can be anything such as a system, network, or browser issue. Failure can also occur due to many exceptions such as “NoSuchElementException”, “TimeOutException”, “StaleElementReferenceException”.

  • After this as an automation engineer, you have to analyze each failure, do the RCA, and rerun the failed testcases again. TestNG has given us relief in this case too using the TestNG IRetryAnalyzer interface.

  • Firstly create a class “Retry” which implements IRetryAnalyzer.

Retry Class


  1. retryCount =0
  2. maxRetryCount=3
  3. You must implement unimplemented methods of the Retry class (i.e retry method)
public boolean retry(ITestResult result) {
 // TODO Auto-generated method stub
 return false;

Important note for class Retry: “Retry” can be changed as per your project need. However “retry” method name should not be changed

In case, you want to decrease or increase the re-run number of test cases, you need to change the maxRetryCount value. In this example, failed test cases will run 3 times till it passes. In case it fails the third time, test execution will stop and TestNG will mark this case as failed.

  • Now there are 2 ways depending on your project need how you would like to use the retry method
  1. At test level (Using retryAnalyzer attribute in the @Test annotation):

Associate your test cases with IRetryAnalyzer. In order to do this, you need to use the method below.

@Test(retryAnalyzer = Retry.class)
public void testCase() {
  1. At the run time (Using Retry Class with ITestAnnotationTransformer Interface)
  • Due to the static nature of annotations, recompilation is needed when you want to change values. You can override this behavior at runtime with the IAnnotationTransformer listener.

  • IAnnotationTransformer is a TestNG listener which allows you to modify TestNG annotations and configure them further during runtime.

  • You must implement the unimplemented method of RetryListener class. (i.e transform method)

public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
 // TODO Auto-generated method stub

Imp note for class RetryListener: “RetryListener” can be changed as per your project need. However “transform” method name should not be changed

  • transform method is called for every test during the test run. We can use this listener for our retry analyzer as shown below:

Transform Method

  • Finally, we will add this listener to our testng.xml file.
 <listener class-name= ""/>
  • Run the testng.xml file and you will see the results as follows:


  • Here as we have mentioned “maxRetryCount=3”, “verifyMovieMetaDataPage” ran thrice before declaring it as a fail. And in the fourth attempt, it finally declared it a fail.