2013-12-13 60 views
0

我想在每次測試後檢查一些外部日誌文件,如果在執行過程中出現錯誤。拋出AfterMethod中的異常不起作用,因爲TestNG對它的處理方式不同:它只會使配置方法失敗,而不會使前面的測試失敗。如何在AfterMethod中的TestNG中進行測試失敗?

我的做法是這樣的:

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

    // doing other cleanUp-tasks 
} 

但儘管如此,我的Eclipse TestNG插件說測試通過。

是否有可能(以及如何)在配置方法中失敗測試(而不僅僅是配置方法)?

回答

2

嘗試從類org.testng.Reporter setCurrentTestResult()方法:

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

這可能爲時已晚,但我可以幫助別人誰正在尋找這一點。

您可以使用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。

+0

你有可能在java中提供相同的上述代碼嗎?謝謝! – OverrockSTAR

+0

我試圖在java中做以下事情 - 我想要做的是,如果TC失敗(我再試兩次),然後將它們標記爲第一個兩個FAILED CASE的SKIP,最後一個也失敗,然後將其報告爲FAILED。代碼:'ITestContext tc = Reporter.getCurrentTestResult()。getTestContext(); \t \t \t tc.getFailedTests()。getAllMethods()。remove(Reporter.getCurrentTestResult()。getMethod()); \t \t \t result.setStatus(ITestResult.SKIP); \t \t \t tc.getSkippedTests()。addResult(result,Reporter.getCurrentTestResult()。getMethod());'pls check – OverrockSTAR

相關問題