簡體   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