繁体   English   中英

testng在测试中保留顺序

[英]testng preserve order in tests

我有几个必须使用testng执行的类文件,当我将<classes>标记中的所有内容合并在一起时,由于执行失败,执行变得随机了。

下面是我的testng文件

<suite name="shakeout" parallel="tests" thread-count="1">

  <test name="test1" preserve-order="true">
  <parameter name="deviceCategory" value="iPhone">
  <parameter name="deviceId" value="<IMEI NO>">
  <classes>
    <class name="com.test1.setup.SetUp">
    <class name="com.test2.signin.SignIn">
  <classes>
</test>
</suite>

这里必须先执行“ Setup”类,然后再执行“ SignIn”,但是执行是随机发生的,并且整个测试用例都失败了。

我创建了AssignTestPriorityTransformer来解决此问题。

从主要班级致电:

TestNG ng = new TestNG();
ng.setAnnotationTransformer(new AssignTestPriorityTransformer());
ng.run();

这是课程:

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javassist.*;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class AssignTestPriorityTransformer implements IAnnotationTransformer
{
    static ClassPool s_ClassPool = ClassPool.getDefault(); 
    static Map<String, Integer> classesForPriority = new HashMap<String, Integer>();
    static Map<String, Integer> methodsForPriority = new HashMap<String, Integer>();
    static int num=1;

    public void transform(ITestAnnotation p_annotation, Class p_testClass, Constructor p_testConstructor, Method p_testMethod)
    {
        int keyValue;
        //every class gets number which will be added in front of priority number
        if (classesForPriority.isEmpty())
        {
            classesForPriority.put(p_testMethod.getDeclaringClass().getCanonicalName(), num);
            keyValue=num;
            num++;
        }
        else
        {
                if (classesForPriority.get(p_testMethod.getDeclaringClass().getCanonicalName())==null){
                    classesForPriority.put(p_testMethod.getDeclaringClass().getCanonicalName(), num);   
                    keyValue=num;
                    num++;
                }
                else 
                    keyValue=classesForPriority.get(p_testMethod.getDeclaringClass().getCanonicalName());
        }


        // get the order number of method in the class
        int priorityInt = getMethodLineNumber(p_testMethod);
        //transform number to include class number at the beginning plus additional number for meaking the ordering correct
        String priorityString = Integer.toString(priorityInt);
        if (priorityString.length()==1)
            priorityString=keyValue + "1000" + priorityString;
        else if (priorityString.length()==2)
            priorityString=keyValue + "100" + priorityString;
        else if (priorityString.length()==3)
            priorityString=keyValue + "10" + priorityString;
        else if (priorityString.length()>=4)
            priorityString=keyValue + "1" + priorityString;

        priorityInt = Integer.parseInt(priorityString);
        p_annotation.setPriority(priorityInt);  
        //p_annotation.setPriority(getMethodLineNumber(p_testMethod));     
        //System.out.println(p_testMethod.getDeclaringClass().getCanonicalName() + "." + p_testMethod.getName() +  " has priority: " + priorityInt);
    }
    private int getMethodLineNumber(Method p_testMethod)
    {
        try
        {
            CtClass cc = s_ClassPool.get(p_testMethod.getDeclaringClass().getCanonicalName());
            CtMethod methodX = cc.getDeclaredMethod(p_testMethod.getName());
            //System.out.println("*******" + p_testMethod.getName() +  " has priority " + methodX.getMethodInfo().getLineNumber(0) + "*******");
            //populate map with method names and orders
            CtMethod[] m = cc.getDeclaredMethods();
            if (methodsForPriority.get(methodX.toString())==null) {
                for (int i = 0; i < m.length; i++) {
                    methodsForPriority.put(m[i].toString(), i+1);
                    //System.out.println(m[i].toString() + methodsForPriority.get(m[i].toString()));
                }
            }
            //int methodNumberInClass = methodsForPriority.get(methodX.toString());
            return methodsForPriority.get(methodX.toString());//methodX.getMethodInfo().getLineNumber(0);        
        }
        catch(Exception e)
        {
            throw new RuntimeException("Getting of line number of method "+p_testMethod+" failed", e);
        }
    }
}

为什么不在suite标记中带有save-order =“ true”属性的不同“测试”块中定义它们(必须先执行“ Setup”类,然后执行“ SignIn”)?

在下面使用:

<test name="Automation" preserve-order="true"  enabled="true">

如果您使用一个@Test依赖于另一个@Test,则保留顺序将不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM