• testng 不同类中使用priority后,没有按照类的顺序执行


    1.使用两个类,priority都从1开始加

    第一个类

      @Test(priority=1)
        public void testAddInfo() {
            System.out.println("testAddInfo");
        }
        @Test(priority=2)
        public void testPublishInfo() {
            System.out.println("testPublishInfo");
        }
        @Test(priority=3)
        public void testCancleInfo() {
            System.out.println("testCancleInfo");
        }
        @Test(priority=4)
        public void testDeleteInfo() {
            System.out.println("testDeleteInfo");
        }
        @Test(priority=5)
        public void testSearchInfo() {
            System.out.println("testSearchInfo");
        }

    第二个类

      @Test(priority=1)
        public void testAdd() {
            System.out.println("testAdd");
        }
        @Test(priority=2)
        public void testPublish() {
            System.out.println("testPublis");
        }
        @Test(priority=3)
        public void testCancle() {
            System.out.println("testCancle");
        }
        @Test(priority=4)
        public void testDelete() {
            System.out.println("testDelete");
        }
        @Test(priority=5)
        public void testSearch() {
            System.out.println("testSearch");
        }
        

    testng配置

      <test name="test" preserve-order="true">     
            <classes>
              <class name="cases.testeoder"></class> 
              <class name=".cases.testeoder2"></class>         
            </classes>
        </test>

    运行结果

    [RemoteTestNG] detected TestNG version 6.14.3
    testAddInfo
    testAdd
    testPublishInfo
    testPublis
    testCancleInfo
    testCancle
    testDeleteInfo
    testDelete
    testSearchInfo
    testAdd
    testSearch

    从中可以看出如果两个类的priority 都从1开始递增,则在执行的之后,即使指定了类的加载先后顺序,但是不会按照预想的一个类按顺序加载完成再执行另一个类。

    主要原因:priority是在testng开始时候全部加载进去

    测试发现 

    1)将priority 换做dependsOnMethods运行效果也是一样的。

    2)即使给第一个类加上分组也是不行

    解决方法:

    1.参考 https://testerhome.com/topics/14824

    写一个监听类 RePrioritizingListener

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import org.testng.IAnnotationTransformer;
    import org.testng.Reporter;
    import org.testng.annotations.ITestAnnotation;
    
    //Listener to fix TestNG Interleaving issue.  I had to re-write this as the original example I had did not allow for priority to be manually set on a test level.
    public class RePrioritizingListener implements IAnnotationTransformer {
    
        HashMap<Object, Integer> priorityMap = new HashMap<Object, Integer>();
        Integer class_priorityCounter = 10000;
        // The length of the final priority assigned to each method.
        Integer max_testpriorityLength = 4;
    
        public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    
    
            // class of the test method.
            Class<?> declaringClass = testMethod.getDeclaringClass();
            // Current priority of the test assigned at the test method.
            Integer test_priority = annotation.getPriority();
            // Current class priority.
            Integer current_ClassPriority = priorityMap.get(declaringClass);
    
            if (current_ClassPriority == null) {
                current_ClassPriority = class_priorityCounter++;
                priorityMap.put(declaringClass, current_ClassPriority);
            }
    
            String concatenatedPriority = test_priority.toString();
    
            // Adds 0's to start of this number.
            while (concatenatedPriority.length() < max_testpriorityLength) {
                concatenatedPriority = "0" + concatenatedPriority;
            }
    
            // Concatenates our class counter to the test level priority (example
            // for test with a priority of 1: 1000100001; same test class with a
            // priority of 2: 1000100002; next class with a priority of 1. 1000200001)
            concatenatedPriority = current_ClassPriority.toString() + concatenatedPriority;
    
            //Sets the new priority to the test method.
            annotation.setPriority(Integer.parseInt(concatenatedPriority));
    
            String printText = testMethod.getName() + " Priority = " + concatenatedPriority;
            Reporter.log(printText);
            System.out.println(printText);
    
        }
    
    }

    然后在testng.xml配置中添加

        <listeners>
            <listener class-name="tools.testng_Listeners.RePrioritizingListener"></listener>
        </listeners>

    2.添加dependsOnGroups,具体如下 

      第一个类
      @Test(priority=1, groups="1") public void testAddInfo() { System.out.println("testAddInfo"); } @Test(priority=2, groups="1") public void testPublishInfo() { System.out.println("testPublishInfo"); } @Test(priority=3, groups="1") public void testCancleInfo() { System.out.println("testCancleInfo"); } @Test(priority=4, groups="1") public void testDeleteInfo() { System.out.println("testDeleteInfo"); } @Test(priority=5, groups="1") public void testSearchInfo() { System.out.println("testSearchInfo"); }

    第二个类

      @Test(priority=1,dependsOnGroups= {"1"}) public void testAdd() { System.out.println("testAdd"); } @Test(priority=2,dependsOnGroups= {"1"}) public void testPublish() { System.out.println("testPublish"); } @Test(priority=3,dependsOnGroups= {"1"}) public void testCancle() { System.out.println("testCancle"); } @Test(priority=4,dependsOnGroups= {"1"}) public void testDelete() { System.out.println("testDelete"); } @Test(priority=5,dependsOnGroups= {"1"}) public void testSearch() { System.out.println("testSearch"); }
  • 相关阅读:
    单相全桥逆变电路工作过程
    单片机实用工具大全
    电路元件
    IC SPEC相关数据
    庖丁解牛,经典运放电路分析
    microstrip(微带线)、stripline(带状线) 指什么?
    [转]关于时钟线/数据线/地址线上串联电阻及其作用
    正激变换电路工作原理
    从Buck-Boost到Flyback
    [转载].关于耦合电容、滤波电容、去耦电容、旁路电容作用
  • 原文地址:https://www.cnblogs.com/xiaoyii/p/9634733.html
Copyright © 2020-2023  润新知