在 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