如何在 TestNG 類中獲取所有測試方法的列表?
TestNG 支援原生依賴注入。它允許在方法中宣告附加引數。在執行時,TestNG 會自動用正確的值填充這些引數。以下是 TestNG 中一些原生依賴項的列表
ITestContext
XmlTest
Method
ITestResult
這些依賴項有助於根據呼叫位置檢索測試方法的名稱。如果使用者想要檢索將在類中執行的所有測試方法的名稱,最佳位置是 @BeforeClass 或 @AfterClass。@BeforeClass 和 @AfterClass 支援 ITestContext 和 XmlTest。
下表顯示了這些依賴項的完全訪問許可權:
註解 | ITestContext | XmlTest | Method | ITestResult |
---|---|---|---|---|
BeforeSuite | 是 | 否 | 否 | 否 |
BeforeTest | 是 | 是 | 否 | 否 |
BeforeGroups | 是 | 是 | 否 | 否 |
BeforeClass | 是 | 是 | 否 | 否 |
BeforeMethod | 是 | 是 | 是 | 是 |
Test | 是 | 否 | 否 | 否 |
AfterMethod | 是 | 是 | 是 | 是 |
AfterClass | 是 | 是 | 否 | 否 |
AfterGroups | 是 | 是 | 否 | 否 |
AfterTest | 是 | 是 | 否 | 否 |
AfterSuite | 是 | 否 | 否 | 否 |
在這篇文章中,我們將使用**XmlTest**依賴項來展示如何檢索類中所有測試方法的名稱。
場景 1
假設使用者希望在執行之前檢索類中所有測試方法的名稱。在這種情況下,程式碼將寫在 @BeforeClass 中,以檢索將在類中執行的所有測試方法的名稱。
由於 @BeforeClass 在測試套件中每次新類開始執行之前都會執行,因此在每個類中測試方法執行之前,都會列印所有測試方法的名稱。
解決此問題的方法/演算法:
**步驟 1** - 建立兩個 TestNG 類:**NewTestngClass** 和 **OrderofTestExecutionInTestNG**。
**步驟 2** - 在兩個類的 @BeforeClass 中編寫以下程式碼;
public void name(ITestContext context) { System.out.println("in beforeclass of <class name>"); ITestContext[] a = context.getAllTestMethods(); for (ITestContext b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } }
**注意**:**getAllTestMethods()** 獲取所有測試方法的名稱作為物件,而 **getName()** 單獨獲取名稱。
**步驟 3** - 在兩個類中編寫兩個不同的 @Test 方法。
**步驟 4** - 現在建立如下所示的 testNG.xml 來執行 TestNG 類。
**步驟 5** - 執行 testNG.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。
示例
對公共 TestNG 類“**NewTestngClass**”使用以下程式碼:
src/ NewTestngClass.java
import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; 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"); } @BeforeClass public void name(ITestContext context) { System.out.println("in beforeClass of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
src/OrderofTestExecutionInTestNG.java:
import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.annotations.BeforeClass; 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"); } @BeforeClass public void name(ITestContext context) { System.out.println("in beforeClass of OrderofTestExecutionInTestNG"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
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 beforeClass of NewTestngClass testCase1 testCase2 in test case 1 of NewTestngClass in test case 2 of NewTestngClass in beforeClass of OrderofTestExecutionInTestNG testCase3 testCase4 in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================
場景 2
假設使用者希望在執行後檢索類中所有測試方法的名稱。在這種情況下,程式碼將寫在 **@AfterClass** 中,以檢索在類中執行的所有測試方法的名稱。
由於 @AfterClass 在測試套件中每次新類完成執行後都會執行,因此在每個類中測試方法執行之後,都會列印所有測試方法的名稱。
解決此問題的方法/演算法:
**步驟 1** - 建立兩個 TestNG 類,例如 **NewTestngClass** 和 **OrderofTestExecutionInTestNG**。
**步驟 2** - 在兩個類的 @AfterClass 中編寫以下程式碼:
public void name(ITestContext context) { System.out.println("in beforeclass of "); ITestContext[] a = context.getAllTestMethods(); for (ITestContext b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } }
**注意** - **getAllTestMethods()** 獲取所有測試方法的名稱作為物件,而 **getName()** 單獨獲取名稱。
**步驟 3** - 在兩個類中編寫兩個不同的 @Test 方法
**步驟 4** - 現在建立如下所示的 testNG.xml 來執行 TestNG 類。
**步驟 5** - 執行 testNG.xml 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。
示例
對公共 TestNG 類“**NewTestngClass**”使用以下程式碼:
src/ NewTestngClass.java
import org.testng.annotations.AfterClass; import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; 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"); } @AfterClass public void name(ITestContext context) { System.out.println("in AfterClass of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
src/OrderofTestExecutionInTestNG.java:
import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.annotations.BeforeClass; 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"); } @AfterClass public void name(ITestContext context) { System.out.println("in AfterClass of OrderofTestExecutionInTestNG"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
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 test case 1 of NewTestngClass in test case 2 of NewTestngClass in AfterClass of NewTestngClass testCase1 testCase2 in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG in AfterClass of OrderofTestExecutionInTestNG testCase3 testCase4 =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================