问题描述
我试图通过以下方式注释为@Test的反射来执行我的测试脚本:
I have trying to execute my test script with the help of reflections which has annotated as @Test as following way:
Class<?> className = Class.forName(format); //Load the class name at runtime
Constructor<?> customConstructor = className.getConstructor(WebDriver.class); //Create customized constructor and initalize driver from testbase
Method[] method = className.getMethods(); //Call the list of methods in current class file
for (Method me : method) {
if (me.getName().startsWith("test")) { //Check wheather the class prefix as test
Method getMethods = Class.forName(format).getDeclaredMethod(me.getName()); //Loading all the methods at runtime.
if(getMethods.isAnnotationPresent(Test.class))
{
//The method which is annotated @Test will execute here, using invoke() method of reflection.
}
}
但问题是无法运行@根据优先级值测试方法。它随机执行。任何人都可以告诉我如何根据优先级值运行@test方法。
But, the problem is not able to run the @Test methods as per priority value. Its executing randomly. Can anybody tell me how could I run the @test methods based on priority value.
此外,我尝试使用dependsOnMethods。但它仍在随机执行。
Also, I tried the same with dependsOnMethods. But still Its randomly executing.
示例代码:
package com.test.build;
import com.test.build.ClassA;
import com.test.build.ClassB;
import java.lang.reflect.*;
import java.util.Scanner;
import org.testng.annotations.Test;
public class ParentClass {
@Test
public void executeTestMetods() throws Exception {
Scanner scan = new Scanner(System.in);
System.out.println("Type package name");
String name = scan.next();
Class<?> class1 = Class.forName(name);
Method[] method = class1.getMethods();
for (Method me : method) {
if (me.isAnnotationPresent(Test.class)) {
if (me.getName().startsWith("test")) {
System.out.println(me.getName());
}
}
}
scan.close();
}
}
ClassA
package com.test.build;
import org.testng.annotations.Test;
@Test(singleThreaded = true)
public class ClassA {
@Test(priority=0)
public void test1()
{
System.out.println("class A");
}
@Test(priority=1)
public void test2()
{
System.out.println("Class A second method");
}
@Test(priority=2)
public void test3()
{
System.out.println("class A");
}
@Test(priority=3)
public void test4()
{
System.out.println("Class A second method");
}
@Test(priority=4)
public void test5()
{
System.out.println("class A");
}
@Test(priority=5)
public void test6()
{
System.out.println("Class A second method");
}
}
输出:
输入包名称
com.test.build.ClassA
test3
test4
test5
test6
test1
test2
已通过:executeTestMetods
Type package namecom.test.build.ClassAtest3test4test5test6test1test2PASSED: executeTestMetods
======================== =======================
默认测试
=============================================== Default test
输出未按优先级正确执行,并显示随机调用。如何使序列执行?
The output is not properly executed as per priority, and shows randomly invoked. How to make it sequence execution?
推荐答案
TestNG为收件箱提供了一个专门的功能来做你想做的事:。
TestNG provides inbox a dedicated feature to do what you want: Annotation transformers.
这篇关于如何使用TestNG + JAVA Reflection设置测试方法执行的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!