zoukankan      html  css  js  c++  java
  • 关于使用testng的retry问题

    总体是利用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();
    } }

  • 相关阅读:
    Silverlight与WPF开发人员入门应该知道的十件事
    Silverlight4.0教程之使用鼠标右键事件和滚轮事件
    Silverlight实用窍门系列:4.Silverlight 4.0添加鼠标右键菜单和Silverlight全屏模式的进入退出。【附带源码实例】
    将moss 2007的模板文件导入到moss 2010
    Silverlight4.0教程之打印报表和图形
    .NET MessageBox 网页弹出消息框
    使用sharepoint自带的文本编辑器1
    C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):目录
    推荐一个非常好的在线免费的 Photoshop
    Silverlight游戏设计(Game Design):目录
  • 原文地址:https://www.cnblogs.com/shenguo/p/3958225.html
Copyright © 2011-2022 走看看