Run test based on defined priority with multiple classes through test.xml file

前端 未结 3 1500
不思量自难忘°
不思量自难忘° 2021-01-26 06:26

I have 3 classes for with 3 tests each.

Class 1

@Test( priority = 1 )
public void testA1() {
    System.out.println(\"testA1\");
}

@Test( priority = 2         


        
3条回答
  •  萌比男神i
    2021-01-26 07:03

    The seen behavior is the expected one.

    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.

    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 intercept(List methods, ITestContext context) {
        // 1. Group by instance/class
        Map, List> map = ...
        for (IMethodInstance method : methods) {
          map.get(method.getInstance().getClass()).add(method);
        }
    
        List result = ...
        // 2. Order methods from an instance/clas according to their priority
        for(Map.Entry entry : map.entries()) {
          List m = entry.value();
          Collections.sort(m, new Comparator() {
            public int compare(IMethodInstance o1, IMethodInstance o2) {
              return o1.getMethod().getPriority() - o2.getMethod().getPriority()
            }
          });
          result.addAll(m);
        }
    
        // 3. Return the result
        return result;
      }
    }
    

提交回复
热议问题