如何在 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 | 是 | 否 | 否 | 否 |
在本文中,我们将使用 **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>
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
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 ===============================================