我已經創建了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);
}
}
}
有時候當然不可能避免,但是,你知道,測試的訂單要求是代碼味道。這種排序,具體來說,被稱爲[時間耦合](https://www.google.com/search?q=temporal+coupling)。在你的情況下,它可能在代碼中,或者它可能只在測試中。無論如何,這可能是值得處理的。 – acdcjunior 2014-09-28 17:48:33
爲了在一個序列中執行需要做些什麼改變? – Badrinath 2014-09-28 17:59:12