Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在testng的@Aftertest中获取testcase状态_Testng - Fatal编程技术网

如何在testng的@Aftertest中获取testcase状态

如何在testng的@Aftertest中获取testcase状态,testng,Testng,如何在TestNg的@AfterTest注释中获取测试用例状态。如果测试用例在@postest中通过或失败,我需要采取一些措施 @AfterTest(alwaysRun=true) public void teardown(ITestContext test) throws Exception { String testcase = test.getCurrentXmlTest().getParameter("id"); } 问题在于ITestContext是全局的,而不是特定于测试的

如何在TestNg的@AfterTest注释中获取测试用例状态。如果测试用例在@postest中通过或失败,我需要采取一些措施

@AfterTest(alwaysRun=true)
public void teardown(ITestContext test) throws Exception {

  String testcase = test.getCurrentXmlTest().getParameter("id");
} 

问题在于
ITestContext
是全局的,而不是特定于测试的。您需要的是有一个侦听器,并让它执行
OnTestFailure


监听器提供了很多可能性,因此您可能希望对此进行研究

如果想要“更快”的解决方案,可以使用
ITestResult.getStatus()

以下是after方法的一个示例:

@AfterMethod
protected void afterMethod(ITestResult result, ITestContext testContext) {
    if (result.getStatus() == ITestResult.FAILURE) {
        //do something in case of fail
    } else {
        //do something else for success
    }
}

不要忘记在参数中传递ITestResults!