通过test.xml文件基于定义的优先级使用多个类运行测试 [英] Run test based on defined priority with multiple classes through test.xml file
问题描述
我有3个班级,每个班级有3个测试.
I have 3 classes for with 3 tests each.
第1类
@Test( priority = 1 )
public void testA1() {
System.out.println("testA1");
}
@Test( priority = 2 )
public void testA2() {
System.out.println("testA2");
}
@Test( priority = 3 )
public void testA3() {
System.out.println("testA3");
}
第2类
@Test( priority = 1 )
public void testB1() {
System.out.println("testB1");
}
@Test( priority = 2 )
public void testB2() {
System.out.println("testB2");
}
@Test( priority = 3 )
public void testB3() {
System.out.println("testB3");
}
3级
@Test( priority = 1 )
public void testC1() {
System.out.println("testC1");
}
@Test( priority = 2 )
public void testC2() {
System.out.println("testC2");
}
@Test( priority = 3 )
public void testC3() {
System.out.println("testC3");
}
这是我的XML文件代码.
This is my XML file code.
<test verbose="2" name="hello" group-by-instances="true">
<classes>
<class name="Class1"></class>
<class name="Class2"></class>
<class name="Class3"></class>
</classes>
</test>
这是我的答案 testA1 测试B1 testC1 testA2 testB2 testC2 testA3 testB3 testC3
Here is my Answer testA1 testB1 testC1 testA2 testB2 testC2 testA3 testB3 testC3
但是我的预期答案是 testA1 testA2 testA3 测试B1 testB2 testB3 testC1 testC2 testC3
But my expected answer is testA1 testA2 testA3 testB1 testB2 testB3 testC1 testC2 testC3
在此先感谢您的帮助.
推荐答案
看到的行为是预期的行为.
The seen behavior is the expected one.
实际上,priority
比group-by-instances
更重要( https://github.com/cbeust/testng/blob/master/CHANGES.txt#L48 )
这就是TestNG尊重priority
而不是group-by-instances
的原因.
In fact, priority
is more important that group-by-instances
( https://github.com/cbeust/testng/blob/master/CHANGES.txt#L48)
and that's why TestNG respect priority
instead of group-by-instances
.
要实现预期的行为,您必须用更重要的订单功能(例如dependsOnMethods
:
To achieve your expected behavior, you have to replace priority
by a more important order feature, like dependsOnMethods
:
@Test
public void testA1() {
System.out.println("testA1");
}
@Test( dependsOnMethods = "testA1" )
public void testA2() {
System.out.println("testA2");
}
@Test( dependsOnMethods = "testA2" )
public void testA3() {
System.out.println("testA3");
}
如评论中的要求,如果您真的想要没有强烈依赖的类的优先级",则可以使用方法拦截器,您可以在其中订购所需的方法.在伪代码中,类似:
As asked in the comments, if you really want a "priority on a class without a strong dependency", you can make it yourself with a method interceptor where you can order methods as you want. In pseudo code, something like:
public class PriorityOnClassOrder implements IMethodInterceptor {
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
// 1. Group by instance/class
Map<Class<?>, List<IMethodInstance>> map = ...
for (IMethodInstance method : methods) {
map.get(method.getInstance().getClass()).add(method);
}
List<IMethodInstance> result = ...
// 2. Order methods from an instance/clas according to their priority
for(Map.Entry entry : map.entries()) {
List<IMethodInstance> m = entry.value();
Collections.sort(m, new Comparator<IMethodInstance>() {
public int compare(IMethodInstance o1, IMethodInstance o2) {
return o1.getMethod().getPriority() - o2.getMethod().getPriority()
}
});
result.addAll(m);
}
// 3. Return the result
return result;
}
}
这篇关于通过test.xml文件基于定义的优先级使用多个类运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!