TestNG 7.0+ 判断annotation.getRetryAnalyzerClass(),实现失败自动重跑
TestNG 7.0以上版本已经弃用了annotation.getRetryAnalyzer() 这个方法
// getRetryAnalyzer() is deprecated
想实现测试用例失败自动重跑,需要在@Test注解中添加 retryAnalyzer = xxxRetryAnalyzer.class,才能实现失败用例自动重跑。
那么避免在所有@Test注解中指定retryAnalyzer 并实现 失败自动Retry呢?
直接上code:
先实现一个 RetryListener:
package listener; import org.testng.IAnnotationTransformer; import org.testng.IRetryAnalyzer; import org.testng.annotations.ITestAnnotation; import org.testng.internal.annotations.DisabledRetryAnalyzer; import java.lang.reflect.Constructor; import java.lang.reflect.Method; /** * Customize IAnnotationTransformer * * @author Vincent-Li */ public class DefaultRetryListener implements IAnnotationTransformer { @Override public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { Class<? extends IRetryAnalyzer> retry = annotation.getRetryAnalyzerClass(); if (retry == DisabledRetryAnalyzer.class) { annotation.setRetryAnalyzer(DefaultRetryAnalyzer.class); } } }
再实现一个 IRetryAnalyzer:
package listener; import org.testng.IRetryAnalyzer; import org.testng.ITestResult; import org.testng.Reporter; import utils.ConfUtils; import utils.LeoLogger; /** * Customize IRetryAnalyzer * * @author Vincent-Li */ public class DefaultRetryAnalyzer implements IRetryAnalyzer { private int retryCount = 1; private static final int maxRetryCount = 3; // 控制用例失败自动重试几次 @Override synchronized public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { Reporter.setCurrentTestResult(result); retryCount++; return true; } return false; } }
用于测试的用例FailedTests:
包含4个测试用例,第一个@Test 指定了retryAnalyzer Class
package ui.testSample; import listener.DefaultRetryAnalyzer; import org.testng.annotations.Test; /** * Some test failures to be included in the sample output. * * @author Vincent-Li */ @Test(groups = "should-fail") public class FailedTests { @Test(retryAnalyzer = DefaultRetryAnalyzer.class) public void assertionFailure() { assert false : "This test failed."; } @Test public void assertionFailureWithMultilineMessage() { assert false : "This test failed.\nIts message is on multiple lines.\n The last one has leading whitespace."; } @Test public void assertionFailureWithOutput() { assert false : "This test failed."; } @Test public void exceptionThrown() { throw new IllegalStateException("Test failed.", new UnsupportedOperationException()); // Nested cause. } }
用于启动测试的Test.xml,未添加 RetryListener
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite" thread-count="1" parallel="instances" verbose="3"> <test name="Test"> <classes> <class name="ui.testSample.FailedTests"></class> </classes> </test> </suite>
执行Test.xml by TestNG, 查看结果,发现只有第一个用例 实现了自动重跑。
为Test.xml 添加 RetryListener:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite" thread-count="1" parallel="instances" verbose="3"> <listeners> <listener class-name="listener.DefaultRetryListener"/> </listeners> <test name="Test"> <classes> <class name="ui.testSample.FailedTests"></class> </classes> </test> </suite>
再次执行Test.xml,查看结果,发现只有4个用例 都实现了自动重跑,并且每个用例的执行次数为 DefaultRetryAnalyzer.maxRetryCount = 3
如此,不在@Test中指定retryAnalyzer ,只需要在Test.xml添加RetryListener,也能实现失败自动重跑。
自动化测试交流群:617352502