shank087 shank087 - 1 month ago 14
Java Question

How to use "skipFailedInvocations" and "retryAnalyzer" using testNG?

How do we use

"skipFailedInvocations"
and
"retryAnalyzer"
with
@Test
Annotation using testNG?

Request you to provide an example.

Answer

Example code:

Demo test class:

public class DemoClass{

@Test(skipFailedInvocations=true, retryAnalyzer=RetryAnalyzer.class)
public void test(){
    Assert.assertTrue(false);
}

}

Retry Analyzer class:

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

public boolean retry(ITestResult result) { 
        if(count < maxCount) {                     
                count++;                                    
                return true; 
        } 
        return false; 
}
}

Explanations:

If the value for skipFailedInvocations is set true and invocation count (here maxCount in the RetryAnalyzer class) is > 1, then all invocations after a failure will be marked as a SKIP instead of a FAIL.