如何在 TestNG 类中获取所有测试方法的列表?
TestNG 支持原生依赖注入。它允许在方法中声明附加参数。在运行时,TestNG 会自动用正确的值填充这些参数。以下是 TestNG 中一些原生依赖项的列表
ITestContext
XmlTest
Method
ITestResult
这些依赖项有助于根据调用位置检索测试方法的名称。如果用户想要检索将在类中执行的所有测试方法的名称,最佳位置是 @BeforeClass 或 @AfterClass。@BeforeClass 和 @AfterClass 支持 ITestContext 和 XmlTest。
下表显示了这些依赖项的完全访问权限:
注解 | ITestContext | XmlTest | Method | ITestResult |
---|---|---|---|---|
BeforeSuite | 是 | 否 | 否 | 否 |
BeforeTest | 是 | 是 | 否 | 否 |
BeforeGroups | 是 | 是 | 否 | 否 |
BeforeClass | 是 | 是 | 否 | 否 |
BeforeMethod | 是 | 是 | 是 | 是 |
Test | 是 | 否 | 否 | 否 |
AfterMethod | 是 | 是 | 是 | 是 |
AfterClass | 是 | 是 | 否 | 否 |
AfterGroups | 是 | 是 | 否 | 否 |
AfterTest | 是 | 是 | 否 | 否 |
AfterSuite | 是 | 否 | 否 | 否 |
在这篇文章中,我们将使用**XmlTest**依赖项来展示如何检索类中所有测试方法的名称。
场景 1
假设用户希望在执行之前检索类中所有测试方法的名称。在这种情况下,代码将写在 @BeforeClass 中,以检索将在类中执行的所有测试方法的名称。
由于 @BeforeClass 在测试套件中每次新类开始执行之前都会执行,因此在每个类中测试方法执行之前,都会打印所有测试方法的名称。
解决此问题的方法/算法:
**步骤 1** - 创建两个 TestNG 类:**NewTestngClass** 和 **OrderofTestExecutionInTestNG**。
**步骤 2** - 在两个类的 @BeforeClass 中编写以下代码;
public void name(ITestContext context) { System.out.println("in beforeclass of <class name>"); ITestContext[] a = context.getAllTestMethods(); for (ITestContext b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } }
**注意**:**getAllTestMethods()** 获取所有测试方法的名称作为对象,而 **getName()** 单独获取名称。
**步骤 3** - 在两个类中编写两个不同的 @Test 方法。
**步骤 4** - 现在创建如下所示的 testNG.xml 来运行 TestNG 类。
**步骤 5** - 运行 testNG.xml 或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。
示例
对公共 TestNG 类“**NewTestngClass**”使用以下代码:
src/ NewTestngClass.java
import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; 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"); } @BeforeClass public void name(ITestContext context) { System.out.println("in beforeClass of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
src/OrderofTestExecutionInTestNG.java:
import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.annotations.BeforeClass; public class OrderofTestExecutionInTestNG { // test case 3 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 4 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } @BeforeClass public void name(ITestContext context) { System.out.println("in beforeClass of OrderofTestExecutionInTestNG"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().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 = "OrderofTestExecutionInTestNG"/> </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 beforeClass of NewTestngClass testCase1 testCase2 in test case 1 of NewTestngClass in test case 2 of NewTestngClass in beforeClass of OrderofTestExecutionInTestNG testCase3 testCase4 in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================
场景 2
假设用户希望在执行后检索类中所有测试方法的名称。在这种情况下,代码将写在 **@AfterClass** 中,以检索在类中执行的所有测试方法的名称。
由于 @AfterClass 在测试套件中每次新类完成执行后都会执行,因此在每个类中测试方法执行之后,都会打印所有测试方法的名称。
解决此问题的方法/算法:
**步骤 1** - 创建两个 TestNG 类,例如 **NewTestngClass** 和 **OrderofTestExecutionInTestNG**。
**步骤 2** - 在两个类的 @AfterClass 中编写以下代码:
public void name(ITestContext context) { System.out.println("in beforeclass of "); ITestContext[] a = context.getAllTestMethods(); for (ITestContext b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } }
**注意** - **getAllTestMethods()** 获取所有测试方法的名称作为对象,而 **getName()** 单独获取名称。
**步骤 3** - 在两个类中编写两个不同的 @Test 方法
**步骤 4** - 现在创建如下所示的 testNG.xml 来运行 TestNG 类。
**步骤 5** - 运行 testNG.xml 或直接在 IDE 中运行 TestNG 类,或者使用命令行编译并运行它。
示例
对公共 TestNG 类“**NewTestngClass**”使用以下代码:
src/ NewTestngClass.java
import org.testng.annotations.AfterClass; import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; 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"); } @AfterClass public void name(ITestContext context) { System.out.println("in AfterClass of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
src/OrderofTestExecutionInTestNG.java:
import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.annotations.BeforeClass; public class OrderofTestExecutionInTestNG { // test case 3 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 4 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } @AfterClass public void name(ITestContext context) { System.out.println("in AfterClass of OrderofTestExecutionInTestNG"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().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"/> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
输出
in test case 1 of NewTestngClass in test case 2 of NewTestngClass in AfterClass of NewTestngClass testCase1 testCase2 in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG in AfterClass of OrderofTestExecutionInTestNG testCase3 testCase4 =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================