Build a capability such that configuration methods can be retried if there's a failure in a previous attempt.
This requires that you use TestNG version 7.2.0
(or) higher because this is dependent on the fix for the issue GITHUB-2257.
The following steps can be followed to accomplish this.
org.testng.Configurable
and ensure that all test classes extend this class.run()
method you implement the retry aware logic.import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Custom annotation that signals that a configuration method should be retried.
*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE})
public @interface Retriable {
/**
* @return - How many times should a configuration be retried.
*/
int attempts() default 1;
}
import org.testng.IConfigurable;
import org.testng.IConfigureCallBack;
import org.testng.ITestResult;
public class AbstractTestCase implements IConfigurable {
@Override
public void run(IConfigureCallBack callBack, ITestResult testResult) {
Retriable retriable =
testResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Retriable.class);
int attempts = 1;
if (retriable != null) {
attempts = retriable.attempts();
}
for (int attempt = 1; attempt <= attempts; attempt++) {
callBack.runConfigurationMethod(testResult);
if (testResult.getThrowable() == null) {
break;
}
}
}
}
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class SampleTestCase extends AbstractTestCase {
private int counter = 1;
@BeforeSuite
@Retriable(attempts = 4)
public void beforeClass() {
if (counter <= 3) {
String msg = "Simulating a failure for attempt " + counter++;
System.err.println(msg);
throw new RuntimeException(msg);
}
System.err.println("Finally the configuration passed");
}
@Test
public void testCase() {
System.err.println("Running a testcase");
}
}
Simulating a failure for attempt 1
Simulating a failure for attempt 2
Simulating a failure for attempt 3
Finally the configuration passed
Running a testcase
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
For any queries, log an issue here.
Tags: TestNG