我有几个必须使用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>
这里&#34;设置&#34;必须执行class,然后执行&#34; SignIn&#34;然而执行是随机发生的,整体测试用例都失败了。
答案 0 :(得分:0)
我已经创建了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);
}
}
}
答案 1 :(得分:0)
为什么不在不同的“测试”中定义它们(&#34;设置&#34;必须执行类,然后执行&#34; SignIn&#34;)?具有保留顺序的块=&#34; true&#34;套件标签中的属性?
答案 2 :(得分:0)
使用以下内容:
<test name="Automation" preserve-order="true" enabled="true">
如果您使用一个@Test依赖于另一个@Test,那么保留顺序将无效。