使用的监听类有:IRetryAnalyzer、TestListenerAdapter、IAnnotationTransformer public class Retry implements IRetryAnalyzer { private int retryCnt = 0 ; private int maxRetryCnt = 1 ; @Override public boolean retry(ITestResult iTestRe
使用的监听类有:IRetryAnalyzer、TestListenerAdapter、IAnnotationTransformer
public class Retry implements IRetryAnalyzer { private int retryCnt = 0; private int maxRetryCnt = 1; @Override public boolean retry(ITestResult iTestResult) { if (retryCnt<maxRetryCnt){ retryCnt++; return true; } return false; } // 用于重置retryCnt public void reset() { retryCnt = 0; } }
public class RetryListener implements IAnnotationTransformer { @Override public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) { IRetryAnalyzer retry = iTestAnnotation.getRetryAnalyzer(); if (retry == null) { iTestAnnotation.setRetryAnalyzer(Retry.class); } } }
/** * 使用dataProvider时,每个case运行结束都强制重置重试次数,如果失败,则重试结束后,重置次数 */ public class TestngListener extends TestListenerAdapter { @Override public void onTestSuccess(ITestResult tr) { super.onTestSuccess(tr); // 对于dataProvider的用例,每次成功后,重置Retry次数 Retry retry = (Retry) tr.getMethod().getRetryAnalyzer(); retry.reset(); } @Override public void onTestFailure(ITestResult tr) { super.onTestFailure(tr); // 对于dataProvider的用例,每次失败后,重置Retry次数 Retry retry = (Retry) tr.getMethod().getRetryAnalyzer(); retry.reset(); } }
新建测试类
public class MyTest { @Test(dataProvider = "d1") public void dd(int i) { System.out.println("i="+i); int count = 0; Assert.assertEquals(i,5); } @DataProvider(name="d1") public Object[][] aa() { return new Object[][] { { 11}, }; } }
testng的xml文件加入listener
<test name="test"> <packages> <package name="qjf"></package> </packages> </test> <listeners> <listener class-name="com.crm.Retry.RetryListener"></listener> <listener class-name="com.Retry.TestngListener"></listener> </listeners>
运行该xml文件,失败后又重试了1次,重试的那一次背忽略
参考:https://ntflc.com/2018/10/18/TestNg-Retry-Failed-Tests-with-DataProvider/#more