How can I fail a test in TestNG in an AfterMethod?

后端 未结 2 408
太阳男子
太阳男子 2020-12-18 16:49

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, becau

相关标签:
2条回答
  • 2020-12-18 17:21

    This might be too late but I can help anyone else who is looking for this.

    You can do this using the ITestContext

    For some reason you cant really update a result which is already generated. The workaround is to add the same result, remove it, update it then add it again. The only way I have found to remove a result is to do the add and remove sequence.

    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)
    

    I am using Groovy.

    0 讨论(0)
  • 2020-12-18 17:24

    Try setCurrentTestResult() method from class org.testng.Reporter:

    result.setStatus(ITestResult.FAILURE);
    Reporter.setCurrentTestResult(result);
    
    0 讨论(0)
提交回复
热议问题