0
votes

I started working with TESTNG for the first time few days back.

I implemented a retry analyzer using the IRetryAnalyzer interface which retries a failed test method for a maximum of 3 times.

I have a test method called retryInvoker() which fails twice and succeeds the third time.

The way TestNG reports this is 3 separate test runs, 2 of which failed and 1 succeeded, which is correct actually. Although I just wish to capture it as a single test run, which ultimately succeeded (if it did before the maximum allowed retries ended, which in this case were 3). Or even if it didn't succeed after 3 retries, I wish to report it as a single test run that failed instead of 4 separate test runs, all of which failed.

Any leads on this?

1
Just to add detail, I also implemented a listener which implements the IMethodListener interface and sets the retry analyzer for the test class if it doesn't have it already before the test method is invoked. I read about the ITestListener interface that has methods such as the onFailure() method which may be used and also there could be a logic implemented in the afterInvocation() method of the IMethodListener. - laksh

1 Answers

0
votes

You could try the approach, suggested in this SO answer, removing the duplicates from the test context in case the same method is found either in failed or passed tests:

@Overrride
public void onFinish(ITestContext context) {
    Iterator<ITestResult> failedTestCases = context.getFailedTests().getAllResults().iterator();
    while (failedTestCases.hasNext()) {
        System.out.println("failedTestCases");
        ITestResult failedTestCase = failedTestCases.next();
        ITestNGMethod method = failedTestCase.getMethod();
        if (context.getFailedTests().getResults(method).size() > 1) {
            System.out.println("failed test case remove as dup:" + failedTestCase.getTestClass().toString());
            failedTestCases.remove();
        } else {
            if (context.getPassedTests().getResults(method).size() > 0) {
                System.out.println(
                        "failed test case remove as pass retry:" + failedTestCase.getTestClass().toString());
                failedTestCases.remove();
            }
        }
    }
}