如何在 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 | 是 | 否 | 否 | 否 |
在这篇文章中,我们将使用方法依赖项(在**@BeforeMethod**和**@AfterMethod**中)来展示如何检索测试方法组。但是,任何这些依赖项都可以用于**@BeforeMethod**或**@AfterMethod**。
场景 1
假设用户希望在执行之前检索测试方法的组。在这种情况下,代码将写在**@BeforeMethod**内部以检索测试方法的组。由于**@BeforeMethod**在**@Test**方法之前每次都会执行,因此测试方法的组名称将在执行之前打印,之后将执行测试方法。在这个场景中,我们将实现方法依赖。
解决此问题的步骤/算法
**步骤 1** - 创建一个 TestNG 类**OrderofTestExecutionInTestNG**并编写**@BeforeMethod**方法。
**步骤 1** - 在 @BeforeMethod 中写入以下代码:
public void name(Method method) { Test testClass = method.getAnnotation(Test.class); for (int i = 0; i < testClass.groups().length; i++) { System.out.println(testClass.groups()[i]); } }
**步骤 3** - 在类**OrderofTestExecutionInTestNG**中编写两个不同的@Test方法,并将第一个测试方法的组添加为**test1**和**test2**,而第二个测试方法的组只添加**test2**,如下面的代码所示。
**步骤 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(groups={"test1", "test2"}) public void testCase1() { System.out.println("in test case 1 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(groups={"test2"}) public void testCase2() { System.out.println("in test case 2 of OrderofTestExecutionInTestNG"); } @BeforeMethod() public void name(Method method) { Test testClass = method.getAnnotation(Test.class); for (int i = 0; i < testClass.groups().length; i++) { System.out.println("Group name is: "+testClass.groups()[i]); } } }
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>
输出
Group name is: test1 Group name is: test2 in test case 1 of OrderofTestExecutionInTestNG Group name is: test2 in test case 2 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
场景 2
假设用户希望在执行后检索测试方法的组名称。在这种情况下,代码将写在**@AfterMethod**内部以检索测试方法描述的组。
由于**@AfterMethod**在**@Test**方法之后每次都会执行,因此测试方法的组将在测试方法执行后打印。在这个场景中,我们将实现方法依赖。
解决此问题的步骤/算法
**步骤 1** - 创建一个 TestNG 类**OrderofTestExecutionInTestNG**并编写**@BeforeMethod**方法。
**步骤 2** - 在**@BeforeMethod**中写入以下代码:
public void name(Method method) { Test testClass = method.getAnnotation(Test.class); for (int i = 0; i < testClass.groups().length; i++) { System.out.println(testClass.groups()[i]); } }
**步骤 3** - 在类**OrderofTestExecutionInTestNG**中编写两个不同的**@Test**方法,并将第一个测试方法的组添加为**test1**和**test2**,而第二个测试方法的组只添加**test2**,如下面的代码所示。
**步骤 4** - 现在创建如下所示的**testNG.xml**来运行 TestNG 类。
**步骤 5** - 最后,运行**testNG.xml**或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
使用以下代码作为通用的 TestNG 类,**OrderofTestExecutionInTestNG**:
src/ OrderofTestExecutionInTestNG.java
import org.testng.annotations.*; import java.lang.reflect.Method; public class OrderofTestExecutionInTestNG { // test case 1 @Test(groups={"test1", "test2"}) public void testCase1() { System.out.println("in test case 1 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(groups={"test2"}) public void testCase2() { System.out.println("in test case 2 of OrderofTestExecutionInTestNG"); } @AfterMethod() public void name(Method method) { Test testClass = method.getAnnotation(Test.class); for (int i = 0; i < testClass.groups().length; i++) { System.out.println("Group name is: "+testClass.groups()[i]); } } }
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 Group name is: test1 Group name is: test2 in test case 2 of OrderofTestExecutionInTestNG Group name is: test2 =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================