如何在 TestNG 套件中檢索所有測試方法名稱?
TestNG 支援原生依賴注入。它允許在方法中宣告額外的引數。在執行時,TestNG 會自動使用正確的值填充這些引數。以下是一組 TestNG 中的原生依賴項:
- ITestContext
- XmlTest
- Method
- ITestResult
這些依賴項有助於檢索測試方法的名稱。如果使用者想要檢索所有將要執行的測試方法的名稱,那麼最佳位置是 **@BeforeSuite** 或 **@AfterSuite**。
**@BeforeSuite** 和 **@AfterSuite** 僅支援 **ITestContext**。這些依賴項的完全訪問許可權如下所示:
註解 | ITestContext | XmlTest | Method | ITestResult |
---|---|---|---|---|
BeforeSuite | 是 | 否 | 否 | 否 |
BeforeTest | 是 | 是 | 否 | 否 |
BeforeGroups | 是 | 是 | 否 | 否 |
BeforeClass | 是 | 是 | 否 | 否 |
BeforeMethod | 是 | 是 | 是 | 是 |
Test | 是 | 否 | 否 | 否 |
AfterMethod | 是 | 是 | 是 | 是 |
AfterClass | 是 | 是 | 否 | 否 |
AfterGroups | 是 | 是 | 否 | 否 |
AfterTest | 是 | 是 | 否 | 否 |
AfterSuite | 是 | 否 | 否 | 否 |
在本文中,我們將使用 Method 依賴項來演示如何檢索所有測試方法的名稱。
場景 1
假設使用者希望在執行之前檢索所有測試方法的名稱。在這種情況下,程式碼將寫入 **@BeforeSuite** 中以檢索將在套件中執行的所有測試方法的名稱。
由於 **@BeforeSuite** 首先執行並且對於測試套件僅執行一次,因此所有測試方法的名稱將在執行之前列印。
解決此問題的方法/演算法
**步驟 1** - 建立兩個 TestNG 類,**NewTestngClass** 和 **OrderofTestExecutionInTestNG**。
**步驟 2** - 在任一類中的 **@BeforeSuite** 內寫入以下程式碼;我們將在 **NewTestngClass** 內寫入:
public void name(ITestContext context) { System.out.println("in beforeSuite of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { System.out.println(b.getConstructorOrMethod().getName()); } }
**注意** - **getAllTestMethods()** 將所有測試方法的名稱作為物件獲取,而 **getName()** 則分別獲取測試方法的名稱。
**步驟 3** - 在兩個類 **NewTestngClass** 和 **OrderofTestExecutionInTestNG** 中編寫兩個不同的 @Test 方法。
**步驟 4** - 現在建立如下所示的 **testNG.xml** 來執行 TestNG 類。
**步驟 5** - 最後,執行 **testNG.xml** 或在 IDE 中直接執行 TestNG 類,或者使用命令列進行編譯和執行。
示例
對通用 TestNG 類 **NewTestngClass** 使用以下程式碼
src/ NewTestngClass.java
import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; import org.testng.ITestContext; 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"); } @BeforeSuite public void name(ITestContext context) { System.out.println("in beforeSuite of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { System.out.println(b.getConstructorOrMethod().getName()); } } }
src/OrderofTestExecutionInTestNG.java
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } }
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 = "NewTestngClass"/> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
輸出
in beforeTest of NewTestngClass testCase1 testCase2 testCase3 testCase4 in test case 1 of NewTestngClass in test case 2 of NewTestngClass in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================
場景 2
假設使用者希望在執行後檢索所有測試方法的名稱。在這種情況下,程式碼將寫入 **@AfterSuite** 中以檢索在套件中執行的所有測試方法的名稱。
由於 **@AfterSuite** 在最後執行並且對於測試套件僅執行一次,因此所有測試方法的名稱將在執行後列印。
解決此問題的方法/演算法
**步驟 1** - 建立兩個 TestNG 類,**NewTestngClass** 和 **OrderofTestExecutionInTestNG**。
**步驟 2** - 在任一類中的 **@AfterSuite** 內寫入以下程式碼;我們將在 **NewTestngClass** 內寫入:
public void name(ITestContext context) { System.out.println("in beforeSuite of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { System.out.println(b.getConstructorOrMethod().getName()); } }
**注意** - **getAllTestMethods()** 將所有測試方法的名稱作為物件獲取,而 **getName()** 則分別獲取測試方法的名稱。
**步驟 3** - 在兩個類 **NewTestngClass** 和 **OrderofTestExecutionInTestNG** 中編寫兩個不同的 @Test 方法。
**步驟 4** - 現在建立如下所示的 **testNG.xml** 來執行 TestNG 類。
**步驟 5** - 最後,執行 **testNG.xml** 或在 IDE 中直接執行 TestNG 類,或者使用命令列進行編譯和執行。
程式程式碼
對通用 TestNG 類 **NewTestngClass** 使用以下程式碼:
src/ NewTestngClass.java
import org.testng.annotations.AfterSuite; import org.testng.annotations.Test; import org.testng.ITestContext; 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"); } @AfterSuite public void name(ITestContext context) { System.out.println("in AfterSuite of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { System.out.println(b.getConstructorOrMethod().getName()); } } }
src/OrderofTestExecutionInTestNG.java
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 3 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 4 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } }
testng.xml
這是一個配置檔案,用於組織和執行 TestNG 測試用例。當需要執行有限的測試而不是完整的套件時,它非常方便。
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" 7gt; <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "NewTestngClass"/> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
輸出
in test case 1 of NewTestngClass in test case 2 of NewTestngClass in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG in AfterTest of NewTestngClass testCase1 testCase2 testCase3 testCase4 =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ================================================