TestNG - Failed retry

来源:互联网 发布:givens变换例题矩阵论 编辑:程序博客网 时间:2024/06/06 05:32

TestNG - Failed retry

TestNG - Failed retry

作者: Max.Bai

时间: 2015/01

TestNG 失败重跑功能

1. 创建自己的IRetryAnalyzer

重写retry 方法,获得xml设置的重跑次数,判断失败次数小于重跑次数是返回true


import org.testng.IRetryAnalyzer;import org.testng.ITestResult;public class TestRetryAnalyzer implements IRetryAnalyzer {private static final String TEST_RETRY_COUNT = "testRetryCount";private int currentTry = 0;private int m_maxRetries = 0;public TestRetryAnalyzer() {currentTry=0;m_maxRetries=3;}public int getCount() {return this.currentTry;}public int getMaxCount() {return this.m_maxRetries;}@Overridepublic synchronized boolean retry(ITestResult result) {String maxRetriesStr = result.getTestContext().getSuite().getParameter("maxRetries");                if(maxRetriesStr != null)        {            try                    {            m_maxRetries = Integer.parseInt(maxRetriesStr);            System.out.println("Get failed Retry count from xml:" + m_maxRetries);            }            catch (final NumberFormatException e)            {            System.out.println("NumberFormatException while parsing maxRetries from suite file." + e);            }        }String testClassName = String.format("%s.%s", result.getMethod().getRealClass().toString(),result.getMethod().getMethodName()); if ( !result.isSuccess() )        {            if ( currentTry < m_maxRetries )            {                ++currentTry;                              System.out.println(    "[RETRYING] " + testClassName + " FAILED, " + "Retrying " + currentTry + " time");                return true;            }        } currentTry=0;        return false;}}

2. 添加自己的IRetryAnalyzer到测试用例

有两种方法:

1. 设置用例或者测试类的listener添加失败重跑IRetryAnalyzer listener

2. 添加自己的IAnnotationTransformer,重写transform方法,添加listener到testng的xml

当用例执行时修改用例的注解,添加自己的重跑IRetryAnalyzer listener

public class RetryListener implements IAnnotationTransformer {@SuppressWarnings("rawtypes")@Overridepublic void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,Method testMethod) {IRetryAnalyzer retry = annotation.getRetryAnalyzer();if (retry == null) { annotation.setRetryAnalyzer(TestRetryAnalyzer.class);}}}


3. 注册自己的监听器

在testng.xml中添加step1 and step2 中自己的监听器

    <listeners>              <listener class-name="Test.RetryListener" />      </listeners>  



4. 添加重跑次数设置

在testng.xml中添加重跑参数

<parameter name="maxRetries" value="1"/> 


失败重跑用例总数会增加,慎用。





0 0
原创粉丝点击