如何在 TestNG 中在执行前后检索测试方法名称?


TestNG 支持原生依赖注入。它允许在方法中声明额外的参数。在运行时,TestNG 会自动用正确的值填充这些参数。以下是一组 TestNG 中的原生依赖项

  • ITestContext
  • XmlTest
  • Method
  • ITestResult

这些依赖项有助于检索测试方法名称。可以在测试执行之前或之后检索测试方法名称。

  • 如果用户希望在测试方法执行之前获取其名称,则 **@BeforeMethod** 可以用来检索它。

  • 如果用户想知道刚刚执行了哪个测试方法,则可以使用 **@AfterMethod**。

可以在这些方法中的任何一个中编写实际代码来检索测试方法名称。**@BeforeMethod** 和 **@AfterMethod** 支持所有这些原生依赖项。下表中给出了这些依赖项的完全访问权限 -

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

在本文中,我们将使用 Method 依赖项来演示如何检索测试方法名称。但是,任何这些依赖项都可以用于 **@BeforeMethod** 或 **@AfterMethod**。唯一的变化将是在 **import** 部分,其中应根据使用的原生依赖项导入相应的库。

场景 1

假设用户希望在执行测试方法之前检索其名称。在这种情况下,代码将写入 **@BeforeMethod** 中以检索测试方法名称。

由于 **@BeforeMethod** 在每个 **@Test** 方法之前都会执行,因此测试方法名称将在执行之前打印,然后执行测试方法。

解决此问题的步骤/算法

  • **步骤 1** - 创建一个 TestNG 类 **NewTestngClass** 并编写 **@BeforeMethod** 方法。

  • **步骤 2** - 在 **@BeforeMethod** 中编写以下代码 -

public void name(Method method) { System.out.println("Test name: " + method.getName()); }

**注意** - 可以使用其余 3 个原生依赖项中的任何一个来代替参数 Method。例如,**ITestContext** 或 **XmlTest** 或 **ITestResult**。

  • **步骤 3** - 在 **NewTestngClass** 类中编写两个不同的 @Test 方法。

  • **步骤 4** - 现在创建如下所示的 **testNG.xml** 来运行 TestNG 类。

  • **步骤 5** - 现在,运行 **testNG.xml** 或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。

示例

对通用 TestNG 类 **NewTestngClass** 使用以下代码 -

src/ NewTestngClass.java

import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.lang.reflect.Method; 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"); } @BeforeMethod public void name(Method method) { System.out.println("Test name: " + method.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"/> </classes> </test> </suite>

输出

Test name: testCase1
in test case 1 of NewTestngClass
Test name: testCase2
in test case 2 of NewTestngClass
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

场景 2

假设用户希望在执行测试方法后检索其名称。在这种情况下,代码将写入 **@AfterMethod** 中以检索测试方法名称。由于 **@AfterMethod** 在每个 **@Test** 方法之后都会执行,因此测试方法名称将在其执行后打印。

解决此问题的步骤/算法

  • **步骤 1** - 创建一个 TestNG 类 **NewTestngClass** 并编写 **@AfterMethod** 方法。

  • **步骤 2** - 在 **@AfterMethod** 中编写以下代码

public void name(Method method) { System.out.println("Test name: " + method.getName()); }

**注意** - 可以使用其余 3 个原生依赖项中的任何一个来代替参数 Method。例如,**ITestContext** 或 **XmlTest** 或 **ITestResult**

  • **步骤 3** - 在 **NewTestngClass** 类中编写两个不同的 **@Test** 方法。

  • **步骤 4** - 现在创建如下所示的 **testNG.xml** 来运行 TestNG 类。

  • **步骤 5** - 现在,运行 **testNG.xml** 或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。

示例

对通用 TestNG 类 **NewTestngClass** 使用以下代码 -

src/ NewTestngClass.java

import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; import java.lang.reflect.Method; 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"); } @AfterMethod public void name(Method method) { System.out.println("Test name: " + method.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"/> </classes> </test> </suite>

输出

in test case 1 of NewTestngClass
Test name: testCase1
in test case 2 of NewTestngClass
Test name: testCase2
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

更新于: 2022年1月12日

4K+ 阅读量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告