关于使用TestNG的retry问题

发表于:2014-11-03 11:43

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:什果的果子    来源:51Testing软件测试网采编

  总体是利用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]);
}
}
}
}
...
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号