TestNG中依次运行测试方法?
一个TestNG类可以包含不同的测试,例如test1、test2、test3等。当用户运行包含各种测试的TestNG类时,它会根据提供的名称按字母顺序运行测试用例。但是,用户可以为这些测试分配优先级,以便这些测试可以按照用户的优先级运行。优先级从0开始,并按递增顺序排列。优先级0具有最高优先级,优先级越高,优先级越低,例如1、2、3等。
在本文中,让我们分析一下执行顺序以不同方式发生的情况。
场景1
如果test2(优先级=0)、test1(优先级=1)、test3(优先级=2),则test2将首先运行,然后是test1,依此类推,基于优先级。
解决此问题的方法/算法
步骤1:为TestNG导入org.testng.annotations.Test。
步骤2:编写一个注释为@test的注解。
步骤3:为@test注解创建一个方法,例如test1,并提供优先级=1。
步骤4:分别为test2和test3重复步骤,优先级为0和2。
步骤5:现在创建testng.xml。
步骤6:现在,运行testng.xml或直接在IDE中运行TestNG类,或者使用命令行编译并运行它。
示例
以下代码创建了一个TestNG类,并显示了执行的优先级顺序。
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
@Test(priority=1)
public void test1() {
System.out.println("Starting execution of TEST1");
}
@Test(priority=0)
public void test2() {
System.out.println("Starting execution of TEST2");
}
@Test(priority=2)
public void test3() {
System.out.println("Starting execution of TEST3");
}
输出
Starting execution of TEST2 Starting execution of TEST1 Starting execution of TEST3
场景2
如果test2(优先级=0)、test1(优先级=1)而test3没有优先级,则test2将首先运行,然后是test3,最后是test1。由于test3没有用户定义的优先级,TestNG将其分配为优先级=0,并且按字母顺序test2先于test3。
解决此问题的方法/算法
步骤1:为TestNG导入org.testng.annotations.Test。
步骤2:编写一个注释为@test的注解。
步骤3:为@test注解创建一个方法,例如test1,并提供优先级=1。
步骤4:分别为test2和test 3重复步骤,优先级为0,并且不提供任何优先级。
步骤5:现在创建testng.xml。
步骤6:现在,运行testng.xml或直接在IDE中运行TestNG类,或者使用命令行编译并运行它。
示例
以下代码创建了一个TestNG类,并显示了执行的优先级顺序。
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
@Test(priority=1)
public void test1() {
System.out.println("Starting execution of TEST1");
}
@Test(priority=0)
public void test2() {
System.out.println("Starting execution of TEST2");
}
@Test()
public void test3() {
System.out.println("Starting execution of TEST3");
}
输出
Starting execution of TEST2 Starting execution of TEST3 Starting execution of TEST1
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP