zoukankan      html  css  js  c++  java
  • 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();
        }
    }
  • 相关阅读:
    Python函数式编程:内置函数map()使用说明
    Python 内置函数 range的使用
    Python的局部变量和全局变量
    Python 数据处理扩展包: pandas 模块的DataFrame介绍(读写数据库的操作)
    Python 数据处理扩展包: pandas 模块的DataFrame介绍(创建和基本操作)
    python模块 mysql-python安装(在ubuntu系统下)
    Python 数据处理扩展包: numpy 和 pandas 模块介绍
    HTML 5 Web Storage 使用
    CRM的组织架构
    WEB UI做TREE
  • 原文地址:https://www.cnblogs.com/AlwinXu/p/10371930.html
Copyright © 2011-2022 走看看