通过 test.xml 文件基于定义的优先级对多个类运行测试 [英] Run test based on defined priority with multiple classes through test.xml file

查看:23
本文介绍了通过 test.xml 文件基于定义的优先级对多个类运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个课程,每个课程有 3 个测试.

I have 3 classes for with 3 tests each.

一级

@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");
}

第二类

@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");
}

第三类

@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>

这是我的答案测试A1测试B1测试C1测试A2测试B2测试C2测试A3测试B3测试C3

Here is my Answer testA1 testB1 testC1 testA2 testB2 testC2 testA3 testB3 testC3

但我预期的答案是测试A1测试A2测试A3测试B1测试B2测试B3测试C1测试C2测试C3

But my expected answer is testA1 testA2 testA3 testB1 testB2 testB3 testC1 testC2 testC3

在此先感谢您对此的任何帮助.

Thanks in advance for any help on this.

推荐答案

所见行为是预期行为.

事实上,prioritygroup-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.

要实现您的预​​期行为,您必须将priority 替换为更重要的订单功能,例如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆