? QA Design Gurus: Re Run Failed Tests automatically |TestNG

Apr 11, 2015

Re Run Failed Tests automatically |TestNG



We are using TestNG framework to manage and execute UI Tests. UI Tests might failed due to different reasons like connection timed out/browser hang/Network Problem...etc. This kind of issues is not related to Application under Test. In this case, we should re run the tests once again.
TestNG is providing default feature called Retry Analyzer which will re execute test case in case of failure.  TestNG Provides interface called IRetryAnalyzer with method called Retry. We need to override this method as per our requirement.
Example:
1| Assume you have a class file with 10 Methods [@Test]
2| Lets say one got failed due to Network issue
3| The class file, RetryAnalyzer will make the failed test-case to re-run with desired count; But, it display errors for all the test failures during re-run. [Explained in this post]
4| you are ought to add a Listener class, RetryTestListener; it will print the failure only once.
5| If the test got passed in the final count, failure exception won't be thrown in the console.
Test Class
@BeforeTest
public void setUp()
 {
System.out.println("This is Setup Method");
 }

@Test(retryAnalyzer=RetryAnalyzer.class)
public void test01() throws Exception
 {
        // Assert.fail();
 Assert.assertEquals("Failed", "Passed");
 }
RetryAnalyzer.java
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer  {
private int count = 0;
private int maxCount = 4; // set your count to re-run test
protected Logger log;
private static Logger testbaseLog;

static {
    PropertyConfigurator.configure("test-config/log4j.properties");
    testbaseLog = Logger.getLogger("TestclassName");
}

public RetryAnalyzer()
{
    testbaseLog.trace( " ModeledRetryAnalyzer constructor " + this.getClass().getName() );
    log = Logger.getLogger("TestclassName");
}

@Override
public boolean retry(ITestResult result) {
    testbaseLog.trace("running retry logic for  '"
            + result.getName()
            + "' on class " + this.getClass().getName() );
        if(count < maxCount) {                    
                count++;                                   
                return true;
        }
        return false;
}
}

The Test Class is having one test method with RetryAnalyzer class which will invoke RetryAnalyzer  whenever the test fails and RetryAnalyzer class re executes the same test method automatically.

No comments:

Post a Comment