引言
TestNG 是一个很棒的测试框架。它提供了许多功能,可以帮助我们构建健壮且易于维护的框架。接下来我们将学习如何在TestNG中重试失败的测试用例。
在执行自动化测试用例(特别是UI自动化)的过程中,您一定遇到过有一些用例第一遍失败了,重试过又过了。引这种问题原因有很多:网络问题、服务器问题、脚本健壮性不够等,失败的原因不是产品的bug,但是我们要花很多时间定位问题的原因。TestNG提供一个很棒的功能,可以自动重试失败用例,只有一次通过,就判定测试成功。
在我们研究TestNG如何实现失败重试机制之前,我们首先要了解org.testng.IRetryAnalyzer接口。接口的定义为
/**
* Returns true if the test method has to be retried, false otherwise.
*
* @param result The result of the test method that just ran.
* @return true if the test method has to be retried, false otherwise.
*/
public boolean retry(ITestResult result);
这个接口只有一个方法
public boolean retry(ITestResult result);
一旦测试失败,该方法就会被调用。ITestResult的输入参数中包括了测试用例详细信息。如果要重新执行失败的测试用例,则此方法的实现应返回true,否则返回false. 可以在方法内指定重试的次数。
该接口的简单实现如下
package Tests;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer {
int counter = 0;
int retryLimit = 4;
/*
* (non-Javadoc)
* @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult)
*
* This method decides how many times a test needs to be rerun.
* TestNg will call this method every time a test fails. So we
* can put some code in here to decide when to rerun the test.
*
* Note: This method will return true if a tests needs to be retried
* and false it not.
*
*/
@Override
public boolean retry(ITestResult result) {
if(counter < retryLimit)
{
counter++;
return true;
}
return false;
}
}
现在我们有了IRetryAnalyzer的简单实现。从retry方法的实现,可以看出此方法可确保重试失败的测试用例4次。因为我们指定了retryLimit = 4;
让我们看看如何使用它,有两种方法可以在您的测试中使用RetryAnalyzer
- 在@Test的注解中指定retryAnalyzer值
- 实现Listener接口,在运行时添加RetryAnalyzer
在@Test的注解中指定retryAnalyzer值
我们可以参考如下语法实现
@Test(retryAnalyzer=”IRetryAnalyzer Implementing class”). 以下是实现的代码
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test001 {
@Test(retryAnalyzer = Tests.RetryAnalyzer.class)
public void Test1()
{
Assert.assertEquals(false, true);
}
@Test
public void Test2()
{
Assert.assertEquals(false, true);
}
}
现在我有两条测试用例,Test1使用了retryAnalyzer。如果我执行这个测试类,测试结果如下
你会看到Test1执行了4次,在最近一次才被标记为失败,因为用了retryAnalyzer。Test2只执行了一次。
运行时指定retryAnalyzer
第二种方法我们要实现ITestAnnotationTransformer接口,ITestAnnotationTransformer是TestNG Listener的一种。该接口定义如下所示
public class AnnotationTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// TODO Auto-generated method stub
}
}
此接口用于在运行时自动向测试方法添加注解。我们通过如下方法设置RetryAnalyzer
ackage Tests;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class AnnotationTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
annotation.setRetryAnalyzer(RetryAnalyzer.class);
}
除了实现IAnnotationTransformer之外,我们还要在test suite文件里设置listener
<suite name="RetryFailedTests" verbose="1" >
<listeners>
<listener class-name="Tests.AnnotationTransformer"/>
</listeners>
<test name="RetryMulitple">
<classes>
<class name="Tests.Test001"/>
</classes>
</test>
</suite>
现在我们不必在@Test注解中指定retryAnalyzer属性了。更新后测试代码如下所示
package Tests;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Test001 {
@Test
public void Test1()
{
Assert.assertEquals(false, true);
}
@Test
public void Test2()
{
Assert.assertEquals(false, true);
}
}
现在您可以简单地运行下测试用例,Test1和Test2 都分别被执行了4次。
本文暂时没有评论,来添加一个吧(●'◡'●)