问答 [已解决] testng 中的 method 乱序问题解决

在路上 · 2018年06月26日 · 最后由 在路上 回复于 2019年03月20日 · 2140 次阅读

1、问题说明:

出现问题:

2、解决问题方法

(1)顺序控制方法:目前使用了 dependsOnMethods 和 testng.xml 中 methods 中的 include 方法顺序控制
结果:偶尔方法执行顺序混乱,大概 20% 的概率
(2)增加顺序控制方法:priority
结果:无效
(3)增加 RePrioritizingListener
结果:有效,暂时没有发现乱序问题

详细解决方法如下:
package tools.testng_Listeners;
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;

    @Override
    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 中添加 listener,解决了 method 乱序问题,目前未发现乱序问题

<listeners>
        <listener class-name="tools.testng_Listeners.RePrioritizingListener"></listener>
    </listeners>
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
最佳回复

org.testng.IMethodInterceptor

API 中说道:This class is used to alter the list of test methods that TestNG is about to run.

API 链接

我用的 priority ,多个 class 测试集执行时候 会有乱序问题 主要原因是 priority 是在 testng 开始时候一口气加载进去的,所以需要 implement IAnnotationTransformer 对每个 class 的 method priority 进行重新设置. 具体可以参考https://testerhome.com/topics/12045

3楼 已删除
共收到 11 条回复 时间 点赞

没遇到过,用例都很乖;你没设置 methods 级别的多线程吧

北溟 回复

没有,因为是业务流的测试,设置多线程就乱了,所以没设置

org.testng.IMethodInterceptor

API 中说道:This class is used to alter the list of test methods that TestNG is about to run.

API 链接

xinufo 回复

佩服,不重复造轮子大约仅适用与层主这样研究深度或者搜索能力的人,除了反射,我能想到的就是以 “test_01_XXX” 命名 method 的这种蠢笨方法了😂

我用的 priority ,多个 class 测试集执行时候 会有乱序问题 主要原因是 priority 是在 testng 开始时候一口气加载进去的,所以需要 implement IAnnotationTransformer 对每个 class 的 method priority 进行重新设置. 具体可以参考https://testerhome.com/topics/12045

套件 类 方法 顺序 这样能避免并发情况下的乱序的

同一个类中的顺序也没方法级并行,感觉楼主说的几个方法应该都能单独起作用的
有没有把@Test以外的注解都去掉试试

回复

可以详细说一下吗?或者截图看一下你的 testng.xml,看看怎么规避,目前是把我卡住了

正在试你的方法,下午看看情况,因为我的乱序是偶尔出现的,所以需要段时间看看

3楼 已删除

一个类内不同优先级还能不按优先级走?方法级别的并发么?大概率按优先级执行??

nowind 回复

确实有这种情况,看最佳回复,那里有答案

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册