以下是用于实现重试逻辑的类
测试重试类:
public class TestRetry implements IRetryAnalyzer {
int counter=0;
int retryLimit=2;
@Override
public boolean retry(ITestResult result) {
if (counter < retryLimit) {
TestReporter.logStep("Retrying Test " +result.getName()+" for number of times: "+(counter+1));
counter++;
return true;
}
return false;
}
重试监听类:
public class RetryListener implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// TODO Auto-generated method stub
IRetryAnalyzer retry = annotation.getRetryAnalyzer();
if (retry == null) {
annotation.setRetryAnalyzer(TestRetry.class);
}
}}
样品测试:
@Listeners(RetryListener.class)
public class SampleTest {
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) {
for (ITestNGMethod method : context.getAllTestMethods()) {
method.setRetryAnalyzer(new TestRetry());
}
}
@Test(priority=0)
public void firsttest() {
System.out.println();
TestReporter.assertEquals("Test", "Test", "pass");
}
@Test(priority=1, dependsOnMethods="firsttest")
public void secondtest() {
TestReporter.assertEquals("Test", "Test1", "fail");
}
@Test(priority=2,dependsOnMethods="secondtest")
public void thirdtest() {
TestReporter.assertEquals("Test", "Test", "pass");
}
}
当我执行上述测试时,以下是输出 firsttest 被执行并通过 secondtest 依赖于 firsttest 并被执行,它失败 - 重试 3 次并再次失败,因为它取决于 secondtest,所以跳过了第三次测试。
产出达到预期。
问题:因为测试是依赖的。如果其中一个测试失败,我想从头开始执行整个课程。有办法吗?
示例:如果 secondtest 失败,我想再次执行整个类 SampleTest。
谢谢!