【发布时间】:2020-06-03 11:52:56
【问题描述】:
我不通过 testng xml 运行我的测试用例,而是使用
TestListener listener=new TestListener();
XmlSuite suite=new XmlSuite();
suite.setName("Test Results");
suite.setParallel(ParallelMode.NONE);
suite.setThreadCount(Integer.parseInt(TestProperties.THREAD_COUNT.toString()));
List<XmlSuite> suits=new ArrayList<XmlSuite>();
suits.add(suite);
List<XmlPackage> xpackage=new ArrayList<XmlPackage>();
xpackage.add(new XmlPackage(TestProperties.TESTNG_PACKAGE.toString()));
XmlTest test=new XmlTest(suite);
test.setPackages(xpackage);
//test.setParallel(ParallelMode.NONE);
String groups=TestProperties.TESTNG_GROUP.toString();
System.out.println("groups are:"+groups);
String groupArray[]=groups.split(",");
List<String> includedGroups=new ArrayList<String>();
includedGroups.addAll(Arrays.asList(groupArray));
test.setIncludedGroups(includedGroups);
TestNG tng=new TestNG();
tng.setOutputDirectory("test-output");
tng.setXmlSuites(suits);
//tng.addListener((ITestNGListener) listener);
tng.run();
System.exit(0)
由于很多因素,我需要按顺序运行测试。我尝试给出parallelMode.none,保留顺序等,但是我的测试用例以一种通常是数字的奇怪方式运行。例如,在 200 之后,我的代码中的测试用例顺序将是 211,212,213 等,然后是 201,202 等。我需要它按该顺序运行。但是现在,在 200 之后,测试用例运行将是 201。我怎样才能使测试用例按照指定的顺序运行。
我也尝试优先考虑测试用例 211,212 等,但这也没有用。我添加了方法拦截器,但我的行号始终为 1。
public class PriorityInterceptor implements IMethodInterceptor {
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
private int getLineNo(IMethodInstance mi) {
int result = 0;
String methodName = mi.getMethod().getMethodName();
String className = mi.getMethod().getConstructorOrMethod().getDeclaringClass().getCanonicalName();
ClassPool pool = ClassPool.getDefault();
try {
CtClass cc = pool.get(className);
CtMethod ctMethod = cc.getDeclaredMethod(methodName);
System.out.println(methodName);
result = ctMethod.getMethodInfo().getLineNumber(0);
System.out.println("result:"+result+"and method:"+ctMethod);
} catch (NotFoundException e) {
e.printStackTrace();
}
return result;
}
public int compare(IMethodInstance m1, IMethodInstance m2) {
System.out.println(getLineNo(m1) - getLineNo(m2));
return getLineNo(m1) - getLineNo(m2);
}
};
IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
Arrays.sort(array, comparator);
System.out.println("22222222222222222222222222222222222222222222222222222222");
System.out.println(Arrays.asList(array));
return Arrays.asList(array);
}
【问题讨论】:
标签: java selenium automation testng