Java 我怎样才能在一个AfterMethod中通过TestNG中的测试?

Java 我怎样才能在一个AfterMethod中通过TestNG中的测试?,java,testing,testng,Java,Testing,Testng,我想在每次测试之后检查一些外部日志文件,看看在执行过程中是否有错误。在AfterMethod中抛出异常不起作用,因为TestNG处理异常的方式不同:它只会使配置方法失败,而不会使前面的测试失败 我的做法是: @AfterMethod(alwaysRun = true) protected void tearDown(ITestResult result) { if (thereWasAProblemDuringTestExecution()) { result.setSt

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

我的做法是:

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

    // doing other cleanUp-tasks
}
但是,我的EclipseTestNG插件说测试通过了

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

请尝试org.testng.Reporter类中的方法:

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

这可能太晚了,但我可以帮助任何正在寻找这个的人

您可以使用ITestContext执行此操作

由于某些原因,您无法真正更新已生成的结果。解决方法是添加相同的结果,删除它,更新它,然后再次添加它。我发现删除结果的唯一方法是执行添加和删除序列

def testContext = Reporter.currentTestResult.testContext
testContext.passedTests.addResult(result,Reporter.currentTestResult.method)
testContext.passedTests.getAllMethods().remove(Reporter.currentTestResult.method)
result.setStatus(ITestResult.FAILURE)
testContext.failedTests.addResult(result,Reporter.currentTestResult.method)

我正在使用Groovy。

您是否可以用java提供上述相同的代码?谢谢我曾尝试在java中执行以下操作-我想做的是,如果TCs失败(我再重试两次),则将前两个失败案例标记为跳过,最后一个也失败,然后将其报告为失败。代码:ITestContext tc=Reporter.getCurrentTestResult().getTestContext();tc.getFailedTests().getAllMethods().remove(Reporter.getCurrentTestResult().getMethod());result.setStatus(ITestResult.SKIP);tc.getSkippedTests().addResult(result,Reporter.getCurrentTestResult().getMethod())请检查