如何在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);