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

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

问题描述

我有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.

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

要实现预期的行为,您必须用更重要的订单功能(例如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天全站免登陆