Search code examples
seleniumtestngtestng-dataprovidertestng-eclipse

selenium testNG retry with incorrect result account


I am using testNG 6.9.10 that installed in Eclipse. I was trying to use retry to make sure the failed tests could run maxcount times that defined. See below codes.

public class TestRetry implements IRetryAnalyzer {
    private int retryCount = 0;
    private int maxRetryCount = 1;

    public boolean retry(ITestResult result) {

        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }

    @Test(retryAnalyzer = TestRetry.class)
    public void testGenX() {
        Assert.assertEquals("google", "google");
    }

    @Test(retryAnalyzer = TestRetry.class)
    public void testGenY() {
        Assert.assertEquals("hello", "hallo");

    }
}

I got below result:

===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================

But seems like the result count with some problems. I want below:

===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

I tried to defined the listeners to implement it, something like to override the onFinish function. You may find it in http://www.seleniumeasy.com/testng-tutorials/retry-listener-failed-tests-count-update But finally not works.

can someone who had met this could help?


Solution

  • Its working fine, i hope there is some problem on listener usage. I created TestRetry as same like you but with out @Test methods.

     public class TestRetry implements IRetryAnalyzer{
    
    private int retryCount = 0;
    private int maxRetryCount = 1;
    
    @Override
    public boolean retry(ITestResult arg0) {
        // TODO Auto-generated method stub
        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
    
        return false;
    }
    }
    

    Created Listener class

     public class TestListener implements ITestListener{
    
    @Override
    public void onFinish(ITestContext context) {
        // TODO Auto-generated method stub
        Set<ITestResult> failedTests = context.getFailedTests().getAllResults();
        for (ITestResult temp : failedTests) {
            ITestNGMethod method = temp.getMethod();
            if (context.getFailedTests().getResults(method).size() > 1) {
                failedTests.remove(temp);
            } else {
                if (context.getPassedTests().getResults(method).size() > 0) {
                    failedTests.remove(temp);
                }
            }
        }
    }
    
    @Override
    public void onStart(ITestContext arg0) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onTestFailure(ITestResult arg0) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onTestSkipped(ITestResult arg0) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onTestStart(ITestResult arg0) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onTestSuccess(ITestResult arg0) {
        // TODO Auto-generated method stub
    
    }
    

    }

    Finally my test class with those methods

      public class RunTest {
    
    
    @Test(retryAnalyzer = TestRetry.class)
    public void testGenX() {
        Assert.assertEquals("google", "google");
    }
    
    @Test(retryAnalyzer = TestRetry.class)
    public void testGenY() {
        Assert.assertEquals("hello", "hallo");
    
    }
    
    }
    

    Executed this RunTest from testng.xml file by specifying the my custom listener

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite1" parallel="false" preserve-order="true">
    <listeners>
       <listener class-name="com.test.TestListener"/>
    </listeners>
    
    <test name="TestA">
    <classes>
      <class name="com.test.RunTest"/>
    </classes>
     </test> <!-- Test -->
     </suite> <!-- Suite -->
    

    enter image description here

    Please have a try..

    Thank You, Murali