在 TestNG 中,BeforeClass 和 BeforeTest 方法的优先级是什么?
一个 TestNG 类可以包含各种 TestNG 方法,例如 @BeforeTest、@AfterTest、@BeforeSuite、@BeforeClass、@BeforeMethod、@test 等。根据执行顺序,@BeforeTest 首先执行,然后执行 @BeforeClass。但是,如果有多个 TestNG 类和每个类中的多个测试,则这些方法的行为就会很明显。
@BeforeTest
此方法在调用第一个 @Test 方法之前,在整个执行过程中仅执行一次。无论存在多少个 @Test 标签,或者有多少个类具有 @Test 标签,或者多个类具有多个测试标签,都无关紧要。根据 testNG.xml 文件,一旦执行开始,@BeforeTest 方法在第一个 @Test 方法以及 @BeforeClass 和 @BeforeMethod(如果存在)执行之前仅执行一次。
@BeforeClass
此方法每个类执行一次,即 @BeforeClass 的执行次数 = 要执行的 TestNG 类的数量。根据 testNG.xml,如果需要执行 10 个 TestNG 类,则每次在每个类开始运行之前都会执行 @BeforeClass。每个类中存在多少个 @Test 标签都无关紧要。
根据最佳实践,始终建议为所有公共 @Before 方法创建一个单独的类,以便可以根据需要执行这些方法,并避免重复调用。
这两种方法的关键点是 -
@BeforeTest 方法在第一个 @Test 方法之前仅执行一次。
@BeforeClass 在每个类之前执行。
如果在不同的类中存在单独的 @BeforeTest 和 @BeforeClass 方法,则所有 @BeforeTest 方法将首先执行,但 @BeforeClass 方法将根据其各自的类执行。
如果所有测试类都扩展了单独类中存在的公共 @BeforeTest 和 @BeforeClass 方法,则 @BeforeTest 将仅执行一次;但是,相同的 @BeforeClass 方法将在每个类之前执行。
场景 1
当有两个 TestNG 类,并且每个类都有两个 @Test 标签时。每个类都扩展了包含 @BeforeTest 和 @BeforeClass 方法的公共类。在这种情况下,@BeforeTest 将仅执行一次,但是相同的 @BeforeClass 方法将执行两次,每次在每个类之前执行。
解决此问题的方法/算法 -
**步骤 1** - 创建一个公共的 TestNG 类“**beforemethods**”并编写 @BeforeTest 和 @BeforeClass 方法。
**步骤 2** - 创建两个 TestNG 类**(OrderofTestExecutionInTestNG 和 NewTestngClass)**,并让每个类扩展公共类**(beforemethods)**。
**步骤 3** - 在这两个类的每个类中编写两个不同的 @Test 方法。
**步骤 4** - 现在创建如下所示的 testNG.xml 以运行这两个 TestNG 类(而不是公共类)。
**步骤 5** - 运行 testNG.xml 或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。
示例
对公共 TestNG 类“**beforemethods**”使用以下代码 -
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class beforemethods {
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforeTest");
}
}对 TestNG 类“**OrderofTestExecutionInTestNG**”使用以下代码 -
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG extends beforemethods {
// test case 1
@Test
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test
public void testCase2() {
System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
}
}对公共 TestNG 类“**NewTestngClass**”使用以下代码 -
import org.testng.annotations.Test;
public class NewTestngClass extends beforemethods{
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
}testNG.xml
这是一个用于组织和运行 TestNG 测试用例的配置文件。当需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "OrderofTestExecutionInTestNG"/> <class name = "NewTestngClass"/> </classes> </test> </suite>
输出
in beforeTest in beforeClass in test case 1 of OrderofTestExecutionInTestNG in test case 2 of OrderofTestExecutionInTestNG in beforeClass in test case 1 of NewTestngClass in test case 2 of NewTestngClassin afterSuite
场景 2
当有两个 TestNG 类,并且每个类都有两个 @Test 标签以及单独的 @BeforeTest 和 @BeforeClass 方法时。
在这种情况下,所有 @BeforeTest 方法将首先执行,但 @BeforeClass 方法将根据其各自的类执行。
解决此问题的方法/算法 -
**步骤 1** - 创建两个 TestNG 类:**OrderofTestExecutionInTestNG 和 NewTestngClass**
**步骤 2** - 在这两个类的每个类中编写两个不同的 @Test 方法。
**步骤 3** - 在这两个类的每个类中编写不同的 @BeforeTest 方法。
**步骤 4** - 在这两个类的每个类中编写不同的 @BeforeClass 方法。
**步骤 5** - 现在创建如下所示的 testNG.xml 以运行这两个 TestNG 类。
**步骤 6** - 运行 testNG.xml 或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。
示例
对 TestNG 类“OrderofTestExecutionInTestNG”使用以下代码 -
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class OrderofTestExecutionInTestNG {
// test case 1
@Test
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test
public void testCase2() {
System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
}
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass of OrderofTestExecutionInTestNG");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforeTest of OrderofTestExecutionInTestNG");
}
}对公共 TestNG 类“**NewTestngClass**”使用以下代码 -
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class NewTestngClass {
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
}
@Test
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforTest of NewTestngClass");
}
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass of NewTestngClass");
}
}testNG.xml
这是一个用于组织和运行 TestNG 测试用例的配置文件。当需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "OrderofTestExecutionInTestNG"/> <class name = "NewTestngClass"/> </classes> </test> </suite>
输出
in beforTest of NewTestngClass in beforeTest of OrderofTestExecutionInTestNG in beforeClass of OrderofTestExecutionInTestNG in test case 1 of OrderofTestExecutionInTestNG in test case 2 of OrderofTestExecutionInTestNG in beforeClass of NewTestngClass in test case 1 of NewTestngClass in test case 2 of NewTestngClassin
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP