如何在testng中的@Aftertest中获取测试用例状态

时间:2013-09-15 14:19:17

标签: testng

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

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

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

2 个答案:

答案 0 :(得分:0)

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

http://testng.org/doc/documentation-main.html#testng-listeners

答案 1 :(得分:0)

听众提供了很多可能性,因此您可能需要研究一下。

如果需要“快速”解决方案,可以使用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
    }
}