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;
TestNG中的@Test注解包含参数retryAnalyzer,可以在注解中给定重试分析器,如下:
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 {
在构造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();
}
}