总体是利用TestNG里面的IRetryAnalyzer、TestListenerAdapter接口来实现相关问题

1、定义一个自己的retryanalyzer

import org.testng.ITestResult;
import org.testng.util.RetryAnalyzerCount;

//这里集成自抽象类RetryAnalyzerCount,该抽象类实现了IRetryAnalyzer
public class TestRetryAnalyzer extends RetryAnalyzerCount{
public TestRetryAnalyzer(){         
        setCount(1); 

@Override 
public boolean retryMethod(ITestResult arg0) { 
        // TODO Auto-generated method stub 
        return true; 
}
}

2、定义自己的监听器,集成自TestListenerAdapter

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.testng.IResultMap;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import org.testng.ITestNGMethod;
import org.testng.collections.Lists;
import org.testng.collections.Objects;

public class RetryTestListener extends TestListenerAdapter {
private List<ITestNGMethod> m_allTestMethods =
     Collections.synchronizedList(Lists.<ITestNGMethod>newArrayList());
private List<ITestResult> m_passedTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
private List<ITestResult> m_failedTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
private List<ITestResult> m_skippedTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
private List<ITestResult> m_failedButWSPerTests = Collections.synchronizedList(Lists.<ITestResult>newArrayList());
private List<ITestContext> m_testContexts= Collections.synchronizedList(new ArrayList<ITestContext>());
private List<ITestResult> m_failedConfs= Collections.synchronizedList(Lists.<ITestResult>newArrayList());
private List<ITestResult> m_skippedConfs= Collections.synchronizedList(Lists.<ITestResult>newArrayList());
private List<ITestResult> m_passedConfs= Collections.synchronizedList(Lists.<ITestResult>newArrayList());

public synchronized void onTestFailure(ITestResult arg0) {
m_allTestMethods.add(arg0.getMethod());
m_failedTests.add(arg0);
}

@Override
public void onFinish(ITestContext context) {

for(int i=0;i<context.getAllTestMethods().length;i++){
System.out.println("~~~~~~~~~~"+context.getAllTestMethods()[i].getCurrentInvocationCount());
if(context.getAllTestMethods()[i].getCurrentInvocationCount()==2){
System.out.println("~~~~~~~~~~~~~~~~~"+context.getAllTestMethods()[i].getParameterInvocationCount());
System.out.println(context.getAllTestMethods()[i].ignoreMissingDependencies());
if
(context.getFailedTests().getResults(context.getAllTestMethods()[i]).size()
== 2 ||
context.getPassedTests().getResults(context.getAllTestMethods()[i]).size()
== 1){

context.getFailedTests().removeResult(context.getAllTestMethods()[i]);

}
}
}
}

...
}

3、在测试申明中说明使用retry

@Test(retryAnalyzer = TestRetryAnalyzer.class)

4、加入一个我们自己的监听器

public class RetryTest {
public static void main(String args[]){
TestNG tng = new TestNG();
RetryTestListener rtl = new RetryTestListener();
XmlSuite xs = new XmlSuite();
Parser parser = new Parser("./testxml/temp.xml");
List<XmlSuite> suites = new ArrayList<XmlSuite>();
try {
suites = parser.parseToList();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
tng.setXmlSuites(suites);
tng.addListener(rtl);
tng.run();
}
}

最新文章

  1. Java基础之处理事件——添加菜单图标(Sketcher 8 with toolbar buttons and menu icons)
  2. eclipse注释模板
  3. php多维数组化一维数组
  4. C#-异常处理:tyr,catch,finally ---ShinePans
  5. Eclipse 常用插件
  6. java基础知识回顾之---java String final类之intern方法
  7. 域名的a记录转过来他的公网ip
  8. 检索表中所有列的名称、DB中的用户表
  9. Excel导入导出帮助类
  10. Php面向对象 – 继承和重写
  11. spring boot Websocket(使用笔记)
  12. 模型加速[tensorflow&amp;tensorrt]
  13. 微信H5支付坑一--手续费未结算
  14. [转]phpredis中文手册
  15. window.location.href刷新页面
  16. MySql数据库学习笔记(3)
  17. python 自动获取手机短信验证码
  18. [Aaronyang] 写给自己的WPF4.5 笔记16[多线程]
  19. 转换CLOB字段类型为VARCHAR2, lob类型不支持的sql语句
  20. 使用sass与compass合并雪碧图(一)

热门文章

  1. Cache应用/任务Mutex,用于高并发任务处理经过多个项目使用
  2. hdu 6154 CaoHaha&#39;s staff
  3. CSS用法
  4. easyui api常用操作
  5. 基于Jquery实现省份、城市、区县三级联动
  6. vue: alias
  7. 趣谈StateServer在Web Garden,Web Farm下的使用
  8. BZOJ2935: [Poi1999]原始生物(欧拉回路)
  9. (转)类的sizeof
  10. test20181021 快速排序