我们使用TestNG来作为自动化测试框架的基础。在运行一批test case之后呢,我们希望能够自动Retry failed的test case。TestNG提供了一个IRetryAnalyzer的接口,在@Test annotation里面可以指定使用Retry类来执行retry的功能。
@Test(retryAnalyzer = TestRetryAnalyzer.class)
TestRetryAnalyzer类实现了IRetryAnalyzer接口。
实现逻辑为:在retry方法里,判断Retry次数是否已经超过指定的最大retry次数。如果没有返回true,否则返回false。
_______________________________________________________________________________ importorg.testng.IRetryAnalyzer;
importorg.testng.ITestResult;
public class TestRetryAnalyzer implements IRetryAnalyzer {
private static final String TEST_RETRY_COUNT = "testRetryCount";
privateint count = 1;
privateintmaxCount = 1;
publicTestRetryAnalyzer() {
String retryMaxCount = System.getProperty(TEST_RETRY_COUNT);
if (retryMaxCount != null) {
maxCount = Integer.parseInt(retryMaxCount);
}
}
publicintgetCount() {
returnthis.count;
}
publicintgetMaxCount() {
returnthis.maxCount;
}
public synchronized boolean retry(ITestResult result) {
String testClassName = String.format("%s.%s", result.getMethod()
.getRealClass().toString(), result.getMethod().getMethodName());
if (count <= maxCount) {
result.setAttribute("RETRY", new Integer(count));
Logging.log("[RETRYING] " + testClassName + " FAILED, "
+ "Retrying " + count + " time", true);
count += 1;
return true;
(Andrew Glover,testearly.com,2006 年 4 月):关亍如何在 TestNG 中重新运行...该公司通过有效的开发者测试策略和连续集成技术 帮劣企业解决软件质量问题,使开发...
(Andrew Glover,testearly.com,2006 年 4 月):关亍如何在 TestNG 中重新运行...该公司通过有效的开发者测试策略和连续集成技术 帮劣企业解决软件质量问题,使开发...
热门文档