如何在AfterMethod中的TestNG测试失败? [英] How can I fail a test in TestNG in an AfterMethod?

查看:548
本文介绍了如何在AfterMethod中的TestNG测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果执行期间出现ary错误,我想在每次测试后检查一些外部日志文件。在 AfterMethod 中抛出异常不起作用,因为TestNG对它的处理方式不同:它只会使配置方法失败而不是前面的测试。

I want to check some external log files after each test, if there were ary errors during execution. Throwing an exception in an AfterMethod doesn't work, because it is handled differently by TestNG: it will just fail the configuration method and not the preceding test.

我的方法是这样的:

@AfterMethod(alwaysRun = true)
protected void tearDown(ITestResult result) {
    if (thereWasAProblemDuringTestExecution()) {
        result.setStatus(ITestResult.FAILURE);
        result.setThrowable(getSomeThrowableSomebodyStoredAnywhere());
    }

    // doing other cleanUp-tasks
}

但是,我的Eclipse TestNG插件说测试通过了。

But still, my Eclipse TestNG plugin says the test passed.

是否有可能(以及如何)使测试失败(而不仅仅是配置方法)在配置方法?

Is it possible (and how) to fail a test (and not only a configuration method) in a configuration method?

推荐答案

尝试 setCurrentTestResult()方法:

result.setStatus(ITestResult.FAILURE);
Reporter.setCurrentTestResult(result);

这篇关于如何在AfterMethod中的TestNG测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆