如何使用TestNG JAVA Reflection设置测试方法执行的优先级
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用TestNG JAVA Reflection设置测试方法执行的优先级,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2380字,纯文字阅读大概需要4分钟。
内容图文
![如何使用TestNG JAVA Reflection设置测试方法执行的优先级](/upload/InfoBanner/zyjiaocheng/703/d15d73d142d44805827a731f452f536c.jpg)
我试图在反射的帮助下执行我的测试脚本,注释为@Test,如下所示:
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方法.它随机执行.任何人都可以告诉我如何根据优先级值运行@test方法.
另外,我尝试使用dependsOnMethods.但它仍在随机执行.
示例代码:
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
PASSED:executeTestMetods
===============================================
默认测试
测试运行:1,失败:0,跳过:0
输出未按优先级正确执行,并显示随机调用.如何使序列执行?
解决方法:
TestNG为收件箱提供了一个专用功能,可以满足您的需求:Annotation transformers.
内容总结
以上是互联网集市为您收集整理的如何使用TestNG JAVA Reflection设置测试方法执行的优先级全部内容,希望文章能够帮你解决如何使用TestNG JAVA Reflection设置测试方法执行的优先级所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。