Software Testing

Retrying Failed TestNG Testcases

Retrying Failed TestNG Testcases

When the test run is complete, we can retry failed Tests From testng-failed.xml

After a TestNG run, it automatically generates:

test-output/testng-failed.xml

You can run this file to execute only failed tests.

TestNG provides an interface called TestNG IRetryAnalyzer that allows you to rerun failed tests automatically.

 

package com.testingdocs.testng.tutorials;




// TestNG Tutorials -  www.TestingDocs.com




import org.testng.IRetryAnalyzer;

import org.testng.ITestResult;



public class RetryFailed implements IRetryAnalyzer {

  private int retry_count=0;

  private int retry_maxcount=2;




  @Override

  public boolean retry(ITestResult result) {

  if(retry_count < retry_maxcount) {

  retry_count++;

  return true;

  }

  return false;

  }

}

 

Test class

Test class that uses the retry mechanism.

package com.testingdocs.testng.tutorials;




//TestNG Tutorials - www.TestingDocs.com




import org.testng.Assert;

import org.testng.annotations.Test;




public class TestNGRetryFailedCases {



  @Test(retryAnalyzer=RetryFailed.class)

  public void failedTest() {

  System.out.println("Test execution ...");

  Assert.assertTrue(false); // force failure

  }

}