51Testing软件测试论坛

标题: testng 中的 method 乱序问题解决 [打印本页]

作者: 测试积点老人    时间: 2019-8-27 11:43
标题: testng 中的 method 乱序问题解决
1、问题说明:
出现问题:
[attach]126343[/attach]
2、解决问题方法
(1)顺序控制方法:目前使用了dependsOnMethods和testng.xml中methods中的include方法顺序控制
结果:偶尔方法执行顺序混乱,大概20%的概率
(2)增加顺序控制方法:priority
结果:无效
(3)增加RePrioritizingListener
结果:有效,暂时没有发现乱序问题

详细解决方法如下:
  1. package tools.testng_Listeners;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Method;
  4. import java.util.HashMap;
  5. import org.testng.IAnnotationTransformer;
  6. import org.testng.Reporter;
  7. import org.testng.annotations.ITestAnnotation;

  8. //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.
  9. public class RePrioritizingListener implements IAnnotationTransformer {

  10.     HashMap<Object, Integer> priorityMap = new HashMap<Object, Integer>();
  11.     Integer class_priorityCounter = 10000;
  12.     // The length of the final priority assigned to each method.
  13.     Integer max_testpriorityLength = 4;

  14.     @Override
  15.     public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {


  16.         // class of the test method.
  17.         Class<?> declaringClass = testMethod.getDeclaringClass();
  18.         // Current priority of the test assigned at the test method.
  19.         Integer test_priority = annotation.getPriority();
  20.         // Current class priority.
  21.         Integer current_ClassPriority = priorityMap.get(declaringClass);

  22.         if (current_ClassPriority == null) {
  23.             current_ClassPriority = class_priorityCounter++;
  24.             priorityMap.put(declaringClass, current_ClassPriority);
  25.         }

  26.         String concatenatedPriority = test_priority.toString();

  27.         // Adds 0's to start of this number.
  28.         while (concatenatedPriority.length() < max_testpriorityLength) {
  29.             concatenatedPriority = "0" + concatenatedPriority;
  30.         }

  31.         // Concatenates our class counter to the test level priority (example
  32.         // for test with a priority of 1: 1000100001; same test class with a
  33.         // priority of 2: 1000100002; next class with a priority of 1. 1000200001)
  34.         concatenatedPriority = current_ClassPriority.toString() + concatenatedPriority;

  35.         //Sets the new priority to the test method.
  36.         annotation.setPriority(Integer.parseInt(concatenatedPriority));

  37.         String printText = testMethod.getName() + " Priority = " + concatenatedPriority;
  38.         Reporter.log(printText);
  39.         System.out.println(printText);

  40.     }

  41. }
复制代码
然后在testng.xml中添加listener,解决了method乱序问题,目前未发现乱序问题
  1. <listeners>
  2.         <listener class-name="tools.testng_Listeners.RePrioritizingListener"></listener>
  3.     </listeners>
复制代码



作者: jingzizx    时间: 2019-8-28 10:10
,感谢分享
作者: qqq911    时间: 2019-8-28 10:34
感谢分享
作者: litingting0214    时间: 2019-8-28 11:42
非常感激
作者: 你好浮戈    时间: 2019-8-28 13:49
谢谢分享,感激~




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2