如何在 TestNG 中執行時獲取測試套件名稱?


TestNG 支援原生依賴注入。它允許在方法中宣告額外的引數。在執行時,TestNG 會自動用正確的值填充這些引數。

以下列出了一些 TestNG 中的原生依賴項:

  • ITestContext
  • XmlTest
  • Method
  • ITestResult

這些依賴項有助於根據呼叫位置檢索測試套件名稱。如果使用者希望在執行之前或執行之後檢索測試套件名稱,最佳位置是 **@BeforeSuite** 或 **@AfterSuite**。

**@BeforeSuite** 和 **@AfterSuite** 支援 **ITestContext**。但是,這些依賴項的完全訪問許可權在以下表格中給出:

註解ITestContextXmlTestMethodITestResult
BeforeSuite
BeforeTest
BeforeGroups
BeforeClass
BeforeMethod
Test
AfterMethod
AfterClass
AfterGroups
AfterTest
AfterSuite

在本文中,我們將使用 **ITestContext** 依賴項來展示如何檢索測試套件名稱。

場景 1

假設使用者希望在執行之前檢索測試套件的名稱。在這種情況下,程式碼將寫入 **@BeforeSuite** 中以檢索將要執行的套件名稱。

由於 **@BeforeSuite** 首先執行,因此套件名稱將在任何測試方法執行之前列印。

解決此問題的方法/演算法

  • **步驟 1** - 建立一個名為 **NewTestngClass** 的 TestNG 類。

  • **步驟 2** - 在類中的 **@BeforeSuite** 內編寫以下程式碼;

public void name(ITestContext context) {
   System.out.println("in beforesuite of NewTestngClass");
   System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().getName());
}
  • **步驟 3** - 在 NewTestngClass 中編寫兩個不同的 **@Test** 方法。

  • **步驟 4** - 現在建立如下所示的 **testNG.xml** 來執行 TestNG 類。

  • **步驟 5** - 執行 **testNG.xml** 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。

示例

對通用 TestNG 類“**NewTestngClass**”使用以下程式碼

src/ NewTestngClass.java

import org.testng.ITestContext;
import org.testng.annotations.*;
public class NewTestngClass {
   // test case 1
   @Test()
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   // test case 2
   @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");
      System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().getName());
   }
}

testng.xml

這是一個用於組織和執行 TestNG 測試用例的配置檔案。當只需要執行有限的測試而不是完整的套件時,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
   <test name = "test1" preserve-order = "true">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

輸出

in beforesuite of NewTestngClass
Test Suite name is:Suite1
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass

===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

場景 2

假設使用者希望在執行後檢索套件的名稱。在這種情況下,程式碼將寫入 **@AfterSuite** 中以檢索已執行的套件名稱,因為 **@AfterSuite** 在執行完成後最後執行一次。

解決此問題的方法/演算法

  • **步驟 1** - 建立一個名為 **NewTestngClass** 的 TestNG 類。

  • **步驟 2** - 在類中的 **@AfterSuite** 內編寫以下程式碼;

public void name(ITestContext context) {
   System.out.println("in beforesuite of NewTestngClass");
   System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().getName());
}
  • **步驟 3** - 在 **NewTestngClass** 中編寫兩個不同的 **@Test** 方法。

  • **步驟 4** - 建立如下所示的 **testNG.xml** 來執行 TestNG 類。

  • **步驟 5** - 執行 **testNG.xml** 或直接在 IDE 中執行 TestNG 類,或者使用命令列編譯並執行它。

示例

對通用 TestNG 類 – **NewTestngClass** – 使用以下程式碼

src/ NewTestngClass.java

import org.testng.ITestContext;
import org.testng.annotations.*;
public class NewTestngClass {
   // test case 1
   @Test()
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   // test case 2
   @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");
      System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().getName());
   }
}

testng.xml

這是一個用於組織和執行 TestNG 測試用例的配置檔案。當只需要執行有限的測試而不是完整的套件時,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
   <test name = "test1" preserve-order = "true">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

輸出

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in aftersuite of NewTestngClass
Test Suite name is:Suite1

===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

更新於: 2022-03-09

2K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告