TestNG 强大的测试框架(6)-接口IRetryAnalyzer,失败用例重复执行
上一章介绍了ant+testng+xlst,其实到了这里,我们就可以直接搭建在Jenkins服务器上,让它跑起来!但是,我之前部署在Jenkins上的UI自动化用例偶尔会报错,我去查看代码,又没有什么问题,再运行一次,它又正常了,其实这种问题,我相信不止我一个人遇到!
说到这里,不得不引进TestNg框架的一个接口:IRetryAnalyzer,它有一个方法retry(ITestResult iTestResult)实现对失败的测试用例多次执行,可以有效的排除上面我说的那种情况。那么怎么来把这个方法给用起来呢?TestNg框架里的Annotion(注解)里可以直接指向该类。
一、下面举个简单的列子
1.IRetryAnalyzer接口的实现,
package com.Retry;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class OverrideRetry implements IRetryAnalyzer{
private int count = 1;
private int max_retry_count = 3;
/*
* OverrideRetry实现接口IRetryAnalyzer的方法,重复执行失败用例
* (non-Javadoc)
* @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult)
*/
@Override
public boolean retry(ITestResult iTestResult) {
System.out.println("执行用例:"+iTestResult.getName()+",第"+count+"次失败");
if(count<max_retry_count){
count++;
return true;
}
return false;
}
}
2.TestNg测试类
@Test(retryAnalyzer= OverrideRetry.class)
public void b(){
System.out.println(2/0);
}
3.运行结果

二、通过上面的demo,我们实现了对失败的测试用例进行二次执行。但是爱思考的同学就会发现,上面的demo不太适合用到真正的自动化测试里,因为每个@test里都要进行retryAnalyzer配置,重复劳动,太累了。
下面给大家介绍TestNg另外一个接口IAnnotationTransformer,判断用例是否达到重试的要求。
1.IRetryAnalyzer接口的实现,
package com.Retry;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.Reporter;
public class OverrideRetry implements IRetryAnalyzer{
private int count = 1; //统计测试用例失败的次数
private int Max_Fail_Count = 3; //测试用例最大失败次数
@Override
public boolean retry(ITestResult iTestResult) {
String msg = "执行用例:"+iTestResult.getName()+"第"+count+"次运行失败";
System.out.println(iTestResult);
Reporter.log(msg);
if(count<Max_Fail_Count){
count++;
return true;
}
return false;
}
}
2.IAnnotationTransformer接口的实现。
package com.Transformer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.annotations.IAnnotationTransformer;
import com.Retry.OverrideRetry;
public class OverrideTransformer implements IAnnotationTransformer{
@SuppressWarnings("rawtypes")
@Override
public void transform(ITestAnnotation iTestAnnotation,
Class class1, Constructor constructor, Method method) {
IRetryAnalyzer iRetryAnalyzer = iTestAnnotation.getRetryAnalyzer();
System.out.println("transform(),iRetryAnalyzer:"+iRetryAnalyzer);
if(iRetryAnalyzer==null){
iTestAnnotation.setRetryAnalyzer(OverrideRetry.class);
}
}
}
3.testng.xml,里面添加了监听器listener,监听类com.Transformer.OverrideTransformer里的重试函数transform(ITestAnnotation iTestAnnotation, Class class1, Constructor constructor, Method method) ,判断下面的测试用例是否失败,达到重试要求.
<?xml version="1.0" encoding="UTF8"?>
<suite name = "suite">
<listeners>
<listener class-name="com.Transformer.OverrideTransformer"></listener>
</listeners>
<test name="CloudPoint" >
<parameter name="resp_code" value="200"/>
<classes name = "Normal">
<class name="com.TestNg.CloudPoint.CP_Normal_1"/>
<class name="com.TestNg.CloudPoint.CP_Normal_2"/>
<class name="com.TestNg.CloudPoint.CP_Exception_androidlost"/>
<class name="com.TestNg.CloudPoint.CP_Exception_kvlost"/>
</classes>
</test>
<test name="DDL" > <parameter name="resp_code" value="200"/> <classes name = "Normal"> <class name="com.TestNg.DDL.DDL_Normal_1"/> </classes> </test>
</suite>
4.运行build.xml,标红部分是重试函数对失败用例的运行结果的输出
Buildfile: D:\java\workspaces\GroupApi\build.xml
clean:
[delete] Deleting directory D:\java\workspaces\GroupApi\bin
compile:
[echo] mkdir
[mkdir] Created dir: D:\java\workspaces\GroupApi\bin
[javac] Compiling 11 source files to D:\java\workspaces\GroupApi\bin
run:
[testng] [TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] transform(),iRetryAnalyzer:null
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
[testng] log4j:WARN Please initialize the log4j system properly.
[testng] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] true
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] true
[testng] excelread:{} is String
[testng] data:{} is String
[testng] D:\java\workspaces\GroupApi
[testng] excel file is D:\java\workspaces\GroupApi\interfacaecase.xlsx
[testng] [TestResult name=StatusCode status=FAILURE method=DDL_Normal_1.StatusCode()[pri:0, instance:com.TestNg.DDL.DDL_Normal_1@50f8360d] output={null}]
[testng] [TestResult name=StatusCode status=FAILURE method=DDL_Normal_1.StatusCode()[pri:0, instance:com.TestNg.DDL.DDL_Normal_1@50f8360d] output={null}]
[testng] [TestResult name=StatusCode status=FAILURE method=DDL_Normal_1.StatusCode()[pri:0, instance:com.TestNg.DDL.DDL_Normal_1@50f8360d] output={null}]
[testng] ===============================================
[testng] suite
[testng] Total tests run: 11, Failures: 1, Skips: 2
[testng] ===============================================
[testng] The tests failed.
[xslt] Processing D:\java\workspaces\GroupApi\test-output\testng-results.xml to D:\java\workspaces\GroupApi\test-output\index1.html
[xslt] Loading stylesheet D:\java\workspaces\GroupApi\test-output\testng-results.xsl
BUILD SUCCESSFUL
Total time: 8 seconds
5.测试报告输出

浙公网安备 33010602011771号