Thursday, 17 March 2016

Rerunning the failed test script using TestNg

Agenda of this blog: To discuss about how to re-run your failed automation scripts without any manual interference.

What is the need: When you write your automation script it might happen that some of your test script may get failed due any reason, it might be due to some internet issues, or UI issues or locator changes of might be some issue with the coding itself or some sporadic issues. So in that case we may need to re-run the failed test case without triggering those again manually. This feature is supported by your Unit test framework TestNg/JUnit, here we will be discussing considering the TestNg as the Unit test Framework.

 Approach we will follow is:


 1.Write a class which will implement  a listener for TestNg i.e IRetryAnalyzer
 2.Use the class in your test classes by using retryAnalyzer which is part of TestNg:
Now let us have a look at the code snippet:

The following class implements the IRetryAnalyzer
package com.testngRetry;


import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;


public class Retry implements IRetryAnalyzer {

           
            public int retryCount = 0;
            public int maxRetryCount = 3;
           
            /**
             * This method is used to retry the failed test case again
             */
            public boolean retry(ITestResult result)
            {
                        if(retryCount < maxRetryCount)
                        {
                                    retryCount++;
                                    return true;
                        }
                        return false;
            }

}

Now the Demo class which is demoing the feature of Re-Run :
package com.testngRetry;

import org.testng.Assert;
import org.testng.annotations.Test;

public class Demo {

            @Test(retryAnalyzer=Retry.class)
            public void testForRetry(){
                       
                        Assert.fail();
                        System.out.println("trying the functionality for testng retry");
            }
           
           
            @Test(retryAnalyzer=Retry.class)
            public void testMethodOne(){
                       
                        System.out.println("This test will get passed");
            }
}


Visit for More Automation Related Discussion:

https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos





No comments:

Post a Comment