第一次写CSDN ,这个问题困扰了我很久,各种查资料,终于把问题解决了,想赶紧记录下,也分享给遇到同样问题,被此问题困扰的同行门~
问题:使用Jenkins执行testng.xml时,因为我写的用例根据不同的功能放在不同的package下,一个pacakage下可能有对应的几十个用例(一个class一个用例集),实在不想把每个class都写到testng中,就使用了<package>标签,统一去执行对应package下的所有class文件,样式如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="接口自动化测试" >
<test name="test:测试接口">
<packages>
<package name="testpro.case.package1" preserve-order="true"/>
</packages>
</suite>
但是在jenkins集成后,所有的用例都是乱序,没有按照我package中文件列表顺序执行,但在(IDEA中执行是有序的)
问题原因是,原因应该是testng会先加载所有@Test注解的priority然后排序
每个测试类的priority肯定是相互独立的,所以这解决方案的思路是重新定义了一下优先级
import org.testng.Reporter;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.annotations.IAnnotationTransformer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
/**
* @Description:
* @Author
* @Date 2021/12/2
* @Version V1.0
**/
public class RePrioritizingNewListener implements IAnnotationTransformer {
HashMap<Object, Integer> priorityMap = new HashMap<Object, Integer>();
Integer class_priorityCounter = 10000;
// The length of the final priority assigned to each method.
Integer max_testpriorityLength = 4;
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// class of the test method.
Class<?> declaringClass = testMethod.getDeclaringClass();
// Current priority of the test assigned at the test method.
Integer test_priority = annotation.getPriority();
// Current class priority.
Integer current_ClassPriority = priorityMap.get(declaringClass);
if (current_ClassPriority == null) {
current_ClassPriority = class_priorityCounter++;
priorityMap.put(declaringClass, current_ClassPriority);
}
String concatenatedPriority = test_priority.toString();
// Adds 0's to start of this number.
while (concatenatedPriority.length() < max_testpriorityLength) {
concatenatedPriority = "0" + concatenatedPriority;
}
// Concatenates our class counter to the test level priority (example
// for test with a priority of 1: 1000100001; same test class with a
// priority of 2: 1000100002; next class with a priority of 1. 1000200001)
concatenatedPriority = current_ClassPriority.toString() + concatenatedPriority;
//Sets the new priority to the test method.
annotation.setPriority(Integer.parseInt(concatenatedPriority));
String printText = testMethod.getName() + " Priority = " + concatenatedPriority;
Reporter.log(printText);
System.out.println(printText);
}
}
上面是网上的一个方法,可以自定义去设置下,给每个class去setPriority(),去设置一个优先级。
我是根据我class文件命名时已经给编了号如:Test_C1001case1的方法。我进行了如下的监听设置
import org.apache.log4j.Logger;
import org.testng.Reporter;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.annotations.IAnnotationTransformer;
import xnypt.util.MySql;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
/**
* @Description: TODO
* @Author
* @Date 2021/12/2
* @Version V1.0
**/
//Listener to fix TestNG Interleaving issue. I had to re-write this as the original example I had did not allow for priority to be manually set on a test level.
public class RePrioritizingListener implements IAnnotationTransformer {
HashMap<Object, Integer> priorityMap = new HashMap<Object, Integer>();
Logger logger = Logger.getLogger(MySql.class);
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
// class of the test method.
Class<?> declaringClass = testMethod.getDeclaringClass();
Integer priority = priorityMap.get(declaringClass);
String name = testMethod.toString().toLowerCase();
int begin = name.indexOf("_");//获取第一个"_"的位置去截取用例序号
String priorityNumber = name.substring(begin+3,begin+6);
if (priority == null) {
priority = Integer.parseInt(priorityNumber);
priorityMap.put(declaringClass, priority);
}
//设置优先级
annotation.setPriority(priority);
logger.info(annotation.getPriority());
String printText = testMethod.getName() + " Priority = " + priority;
Reporter.log(printText);
logger.info(printText);
}
}
设置完成后,再将testng.xml中新增监听
<listeners> <listener class-name="xnypt.common.RePrioritizingListener"></listener> </listeners>
再将test中的属性preserve-order="false"(这个一定要改!!!!!)
然后就成功了