如何在 TestNG 執行前後獲取測試組名稱?
TestNG 支援原生依賴注入。它允許在方法中宣告附加引數。在執行時,TestNG 會自動用正確的值填充這些引數。以下是一組 TestNG 中的原生依賴項
- ITestContext
- XmlTest
- Method
- ITestResult
這些依賴項有助於檢索測試方法的描述(如果已編寫)。可以在測試執行之前或之後檢索測試方法的**組名稱**。
如果使用者希望在測試方法執行之前獲取其組名稱,則**@BeforeMethod**可以用來檢索它。
另一方面,如果使用者想知道剛剛執行的測試方法所屬的組,則可以使用**@AfterMethod**。
可以在這些方法中的任何一箇中編寫實際程式碼來檢索測試方法描述。**@BeforeMethod**和**@AfterMethod**都支援所有這些原生依賴項。下面給出了這些依賴項的完整訪問許可權:
| 註解 | ITestContext | XmlTest | Method | ITestResult |
|---|---|---|---|---|
| BeforeSuite | 是 | 否 | 否 | 否 |
| BeforeTest | 是 | 是 | 否 | 否 |
| BeforeGroups | 是 | 是 | 否 | 否 |
| BeforeClass | 是 | 是 | 否 | 否 |
| BeforeMethod | 是 | 是 | 是 | 是 |
| Test | 是 | 否 | 否 | 否 |
| AfterMethod | 是 | 是 | 是 | 是 |
| AfterClass | 是 | 是 | 否 | 否 |
| AfterGroups | 是 | 是 | 否 | 否 |
| AfterTest | 是 | 是 | 否 | 否 |
| AfterSuite | 是 | 否 | 否 | 否 |
在這篇文章中,我們將使用方法依賴項(在**@BeforeMethod**和**@AfterMethod**中)來展示如何檢索測試方法組。但是,任何這些依賴項都可以用於**@BeforeMethod**或**@AfterMethod**。
場景 1
假設使用者希望在執行之前檢索測試方法的組。在這種情況下,程式碼將寫在**@BeforeMethod**內部以檢索測試方法的組。由於**@BeforeMethod**在**@Test**方法之前每次都會執行,因此測試方法的組名稱將在執行之前列印,之後將執行測試方法。在這個場景中,我們將實現方法依賴。
解決此問題的步驟/演算法
**步驟 1** - 建立一個 TestNG 類**OrderofTestExecutionInTestNG**並編寫**@BeforeMethod**方法。
**步驟 1** - 在 @BeforeMethod 中寫入以下程式碼:
public void name(Method method) {
Test testClass = method.getAnnotation(Test.class);
for (int i = 0; i < testClass.groups().length; i++)
{
System.out.println(testClass.groups()[i]);
}
}**步驟 3** - 在類**OrderofTestExecutionInTestNG**中編寫兩個不同的@Test方法,並將第一個測試方法的組新增為**test1**和**test2**,而第二個測試方法的組只新增**test2**,如下面的程式碼所示。
**步驟 4** - 現在建立如下所示的**testNG.xml**來執行 TestNG 類。
**步驟 5** - 最後,執行**testNG.xml**或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。
程式程式碼
使用以下程式碼作為通用的 TestNG 類,**OrderofTestExecutionInTestNG**:
src/ OrderofTestExecutionInTestNG.java
import org.testng.annotations.*;
import java.lang.reflect.Method;
public class OrderofTestExecutionInTestNG {
// test case 1
@Test(groups={"test1", "test2"})
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test(groups={"test2"})
public void testCase2() {
System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
}
@BeforeMethod()
public void name(Method method) {
Test testClass = method.getAnnotation(Test.class);
for (int i = 0; i < testClass.groups().length; i++) {
System.out.println("Group name is: "+testClass.groups()[i]);
}
}
}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"/>
</classes<
</test<
</suite>輸出
Group name is: test1 Group name is: test2 in test case 1 of OrderofTestExecutionInTestNG Group name is: test2 in test case 2 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
場景 2
假設使用者希望在執行後檢索測試方法的組名稱。在這種情況下,程式碼將寫在**@AfterMethod**內部以檢索測試方法描述的組。
由於**@AfterMethod**在**@Test**方法之後每次都會執行,因此測試方法的組將在測試方法執行後列印。在這個場景中,我們將實現方法依賴。
解決此問題的步驟/演算法
**步驟 1** - 建立一個 TestNG 類**OrderofTestExecutionInTestNG**並編寫**@BeforeMethod**方法。
**步驟 2** - 在**@BeforeMethod**中寫入以下程式碼:
public void name(Method method) {
Test testClass = method.getAnnotation(Test.class);
for (int i = 0; i < testClass.groups().length; i++) {
System.out.println(testClass.groups()[i]);
}
}**步驟 3** - 在類**OrderofTestExecutionInTestNG**中編寫兩個不同的**@Test**方法,並將第一個測試方法的組新增為**test1**和**test2**,而第二個測試方法的組只新增**test2**,如下面的程式碼所示。
**步驟 4** - 現在建立如下所示的**testNG.xml**來執行 TestNG 類。
**步驟 5** - 最後,執行**testNG.xml**或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。
示例
使用以下程式碼作為通用的 TestNG 類,**OrderofTestExecutionInTestNG**:
src/ OrderofTestExecutionInTestNG.java
import org.testng.annotations.*;
import java.lang.reflect.Method;
public class OrderofTestExecutionInTestNG {
// test case 1
@Test(groups={"test1", "test2"})
public void testCase1() {
System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
}
// test case 2
@Test(groups={"test2"})
public void testCase2() {
System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
}
@AfterMethod()
public void name(Method method) {
Test testClass = method.getAnnotation(Test.class);
for (int i = 0; i < testClass.groups().length; i++) {
System.out.println("Group name is: "+testClass.groups()[i]);
}
}
}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"/>
</classes>
</test>
</suite>輸出
in test case 1 of OrderofTestExecutionInTestNG Group name is: test1 Group name is: test2 in test case 2 of OrderofTestExecutionInTestNG Group name is: test2 =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP