如何在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 | 是 | 否 | 否 | 否 |
在本文中,我們將使用Method依賴項(在**@BeforeMethod**中)和**ITestResult**(在**@AfterMethod**中)來演示如何檢索測試方法的描述。但是,任何這些依賴項都可以用於**@BeforeMethod**或**@AfterMethod**。唯一不同的是匯入部分,其中應根據使用的原生依賴項匯入相應的庫。
場景1
假設使用者希望在執行之前檢索測試方法的描述。在這種情況下,程式碼將寫在**@BeforeMethod**中以檢索測試方法描述。
由於**@BeforeMethod**在每個**@Test**方法之前執行,因此測試方法名稱和描述將在執行之前列印,之後將執行**@Test**方法。在這個場景中,我們將實現Method依賴項。
解決此問題的方法/演算法
**步驟1** - 建立一個TestNG類 - **OrderofTestExecutionInTestNG**並編寫**@BeforeMethod**方法。
**步驟2** - 在**@BeforeMethod**中寫入以下程式碼:
public void name(Method method) { System.out.println("Test name is " + method.getName()); System.out.println("Test description is " + method.getAnnotation(Test.class).description()); }
**步驟3** - 在類中編寫兩個不同的@Test方法 - **NewTestngClass**。
**步驟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(description="test case 1 to execute") public void testCase1() { System.out.println("in test case 1 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(description="test case 2 to execute") public void testCase2() { System.out.println("in test case 2 of OrderofTestExecutionInTestNG"); } @BeforeMethod() public void name(Method method) { System.out.println("Test name is: " + method.getName()); System.out.println("Test description is: " + method.getAnnotation(Test.class).description()); } }
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>
輸出
Test name is: testCase1 Test description is: test case 1 to execute in test case 1 of OrderofTestExecutionInTestNG Test name is: testCase2 Test description is: test case 2 to execute in test case 2 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
場景2
當用戶希望在執行後檢索測試方法的描述時。在這種情況下,程式碼將寫在**@AfterMethod**中以檢索測試方法描述。
由於**@AfterMethod**在每個**@Test**方法之後執行,因此測試方法名稱和描述將在測試方法執行後列印。在這個場景中,我們將實現**ITestResult**依賴項。
解決此問題的方法/演算法
**步驟1** - 建立一個TestNG類 - **OrderofTestExecutionInTestNG**並編寫**@BeforeMethod**方法。
**步驟2** - 在**@BeforeMethod**中寫入以下程式碼:
public void name(ITestResult result){ System.out.println("Test Name is: " +result.getMethod().getMethodName()); System.out.println("Test description is: "+result.getMethod().getDescription()); }
**步驟3** - 在類中編寫2個不同的@Test方法 - OrderofTestExecutionInTestNG。
**步驟4** - 現在建立如下所示的testNG.xml來執行TestNG類。
**步驟5** - 現在,執行testNG.xml或直接在IDE中執行TestNG類,或者使用命令列編譯並執行它。
示例
以下程式碼適用於公共TestNG類 - OrderofTestExecutionInTestNG:src/ OrderofTestExecutionInTestNG.java
import org.testng.annotations.*; import org.testng.ITestResult; public class OrderofTestExecutionInTestNG { // test case 1 @Test(description="test case 1 to execute") public void testCase1() { System.out.println("in test case 1 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(description="test case 2 to execute") public void testCase2() { System.out.println("in test case 2 of OrderofTestExecutionInTestNG"); } @AfterMethod public void name(ITestResult result){ System.out.println("Test Name is:" +result.getMethod().getMethodName()); System.out.println("Test description is:" +result.getMethod().getDescription()); } }
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 Test Name is:testCase1 Test description is:test case 1 to execute in test case 2 of OrderofTestExecutionInTestNG Test Name is:testCase2 Test description is:test case 2 to execute =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================