51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1459|回复: 4
打印 上一主题 下一主题

testng 中的 method 乱序问题解决

[复制链接]
  • TA的每日心情
    无聊
    11 小时前
  • 签到天数: 509 天

    连续签到: 5 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2019-8-27 11:43:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点
    1、问题说明:
    出现问题:

    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>
    复制代码


    附件: 您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    12 小时前
  • 签到天数: 2790 天

    连续签到: 5 天

    [LV.Master]测试大本营

    2#
    发表于 2019-8-28 10:10:49 | 只看该作者
    ,感谢分享
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    10 小时前
  • 签到天数: 1496 天

    连续签到: 1 天

    [LV.10]测试总司令

    3#
    发表于 2019-8-28 10:34:42 | 只看该作者
    感谢分享
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    9 小时前
  • 签到天数: 973 天

    连续签到: 5 天

    [LV.10]测试总司令

    4#
    发表于 2019-8-28 11:42:46 | 只看该作者
    非常感激
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    11 小时前
  • 签到天数: 343 天

    连续签到: 5 天

    [LV.8]测试军长

    5#
    发表于 2019-8-28 13:49:17 | 只看该作者
    谢谢分享,感激~
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-10-18 20:30 , Processed in 0.073983 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表