TestNg失败重试机制

TestNg提供了失败重试接口IRetryAnalyzer,需要实现retry方法:

 

package com.shunhe.testngprac.retry;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

/**
 * 设置testng用例失败重试次数
 * Created by shunhe on 2019/2/13.
 */
public class MyRetryAnalyzer implements IRetryAnalyzer {

    private int retryCount = 0;
    private final int MAX_RETRY_COUNT = 3;

    @Override
    public boolean retry(ITestResult iTestResult) {
        if (retryCount < MAX_RETRY_COUNT) {
            retryCount++;
            return true;
        }
        return false;
    }
}

 

TestNG中的@Test注解包含参数retryAnalyzer,可以在注解中给定重试分析器,如下:

 

    @Test(retryAnalyzer = MyRetryAnalyzer.class)
    public void test01() {
        System.out.println("test01");
        Assert.assertTrue(false, "test failed");
    }

 

TestNg程序化调用情况

 

需要实现接口IAnnotationTransformer中的transform方法,对未设置retryanalyzer的@test方法添加响应的retryanalyzer:

 

package com.shunhe.testngprac.retry;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation;

/**
 * 添加重试监听器,如果测试方法中未添加重试分析器,则默认添加MyRetryAnalyzer作为重试分析器
 * Created by shunhe on 2019/2/13.
 */
public class MyRetryListener implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {
        IRetryAnalyzer retryAnalyzer = iTestAnnotation.getRetryAnalyzer();
        if (null == retryAnalyzer) {
            iTestAnnotation.setRetryAnalyzer(MyRetryAnalyzer.class);
        }
    }
}

 

在构造tesng执行对象时,添加响应的listener

 

package com.shunhe.testngprac.controller;

import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlSuite.ParallelMode;
import org.testng.xml.XmlTest;

/**
 * Created by shunhe on 2019/2/13.
 */
public class TestNgController {

    /**
     * TestNG测试程序化调用
     */
    public void executeTests() {
        //构建testng.xml内存对象
        try {
            List<XmlSuite> suites = new ArrayList<XmlSuite>();
            XmlSuite suite = new XmlSuite();
            suite.setName("TestNG Retry Test");
            //添加失败重试
            List<String> suiteListeners = new ArrayList<String>();
            //添加重试监听器
            suiteListeners.add("com.shunhe.testngprac.retry.MyRetryListener");
            suite.setListeners(suiteListeners);

            //类级别的并发
            suite.setParallel(ParallelMode.CLASSES);
            suites.add(suite);

            XmlTest test = new XmlTest(suite);
            test.setName("TestNG Retry Test");

            //添加将要执行的用例
            List<XmlClass> classes = new ArrayList<XmlClass>();

            XmlClass testClass = new XmlClass("com.shunhe.testngprac.tests.DemoTest01");

            classes.add(testClass);

            test.setXmlClasses(classes);

            //TestNG程序化调用
            TestNG testNG = new TestNG();
            testNG.setXmlSuites(suites);
            testNG.run();

        } catch (Exception e) {
            System.out.println("测试用例执行失败: " + e.getMessage());
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        TestNgController testNgController = new TestNgController();
        testNgController.executeTests();
    }
}
posted on 2019-02-13 21:33  Alvin_Xu  阅读(2679)  评论(0编辑  收藏  举报