如何在 TestNG 中获取测试方法的当前状态?


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

  • ITestContext

  • XmlTest

  • Method

  • ITestResult

这些依赖项有助于检索测试执行状态。

通常,@AfterMethod 支持所有这些原生依赖项,并且测试状态可以是成功、失败或跳过。

但是,TestNG 支持以下测试状态,可以通过在正确的位置调用函数来检索。

                                        org.testng.ITestResult

public static final int

FAILURE

2

public static final int

SKIP

3

public static final int

STARTED

16

public static final int

SUCCESS

1

public static final int

SUCCESS_PERCENTAGE_FAILURE

4

在本文中,我们将使用 ITestResult 依赖项来演示如何检索测试状态。

当用户希望在执行后检索测试方法的状态时。在这种情况下,代码将写入 @AfterMethod 中以检索测试方法的状态。

由于 @AfterMethod 在每次 @Test 方法之后执行,因此将打印已执行的测试方法名称的状态。在这里,我们将使用 getStatus() 方法,它返回整数,如上表所示。用户应该使用这些状态代码作为整数来实现字符串。

解决此问题的方法/算法

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

  • 步骤 2:在 @AfterMethod 中编写以下代码。

  • 步骤 3:在类 - NewTestngClass 中编写 3 个不同的 @Test 方法。一个用于成功,一个用于失败,一个用于跳过,如程序代码所示。

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

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

示例

以下代码适用于通用 TestNG 类 - NewTestngClass

src/ NewTestngClass.java

import org.testng.ITestResult; import org.testng.SkipException; import org.testng.annotations.*; public class NewTestngClass { @Test() public void testcase1(){ System.out.println("TestCase 1 - Execution started"); assert true; } @Test() public void testcase2(){ System.out.println("TestCase 2 - Execution started"); assert false; } @Test() public void testcase3(){ System.out.println("TestCase 3 - Execution started"); throw new SkipException("Skipping the testcase 3"); } @AfterMethod() public void status(ITestResult result){ System.out.println("Status of execution is:"+result.getStatus()) ; try { if(result.getStatus() == ITestResult.SUCCESS) { System.out.println("Test case execution status is SUCCESS"); } else if(result.getStatus() == ITestResult.FAILURE) { //Do something here System.out.println("Test case execution status is FAILURE"); } else if(result.getStatus() == ITestResult.SKIP ){ System.out.println("Test case execution status is SKIP"); } } catch(Exception e) { e.printStackTrace(); } } }

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>

输出

TestCase 1 - Execution started
Status of execution is:1
Test case execution status is SUCCESS
TestCase 2 - Execution started

java.lang.AssertionError
	at NewTestngClass.testcase2(NewTestngClass.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
	at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
	at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
	at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
	at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
	at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.util.ArrayList.forEach(ArrayList.java:1259)
	at org.testng.TestRunner.privateRun(TestRunner.java:794)
	at org.testng.TestRunner.run(TestRunner.java:596)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
	at org.testng.SuiteRunner.run(SuiteRunner.java:276)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
	at org.testng.TestNG.runSuites(TestNG.java:1063)
	at org.testng.TestNG.run(TestNG.java:1031)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

Status of execution is:2
Test case execution status is FAILURE
TestCase 3 - Execution started

Test ignored.
Status of execution is:3
Test case execution status is SKIP

===============================================
Suite1
Total tests run: 3, Passes: 1, Failures: 1, Skips: 1
=============================================== 

更新于: 2023年8月17日

232 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告