2017-01-13 32 views
1

我希望我的測試重試5次,但不超過x分鐘。在最後一次調用之後,它將在@After方法中調用mergeReport方法。這裏我的示例代碼:@AfterMethod方法使用invocationTimeOut和invocationCount> 2(TestNG 6.9 - > 6.10)時未執行

@Test(priority = 1, invocationCount = 5, invocationTimeOut = 6000, expectedExceptions = ThreadTimeoutException.class) 
public void test() throws Exception { 
    while (true) { 
     Thread.sleep(1000); 
    } 
} 

@AfterMethod(lastTimeOnly = true, alwaysRun = true) 
public void mergeReport(ITestResult result) throws Exception { 
    System.out.println("Report"); 
} 

測試結果將爲空。但是,當我改變invocationCount 2,測試結果將是:

Report 

我通過網絡搜索,發現類似的問題在2014年,但它仍然沒有答案:https://github.com/cbeust/testng/issues/325

有沒有人遇到了這個問題?

+0

我不確定325是一個類似的問題。你能否開一個新的項目並在那裏提供一個完整的可運行項目? – juherr

+0

我使用TestListenerAdapter和IInvokedMethodListener修復了此問題。如果在測試超時達到invocationCount = 5之前,我的自定義偵聽器將其設置爲5,並且將觸發after方法。 '(iTestResult.getMethod()。getCurrentInvocationCount()

回答

0

你用timeOut而不是invocationTimeout試過嗎?

@Test(priority = 1, invocationCount = 5, invocationTimeout= 20000, expectedExceptions = ThreadTimeoutException.class) 
public void test() throws Exception { 
    while (true) { 
     Thread.sleep(1000); 
    } 
} 

從TestNG的文檔:

invocationTimeOut - >這個測試 應該對毫秒的最大數量累計當時所有的invocationcounts的。如果未指定invocationCount,則此 屬性將被忽略。

timeOut - >此測試應採用的最大毫秒數。

我想你是在超時之後爲你想要的!

然後,你再次提到你想要運行X分鐘。關鍵詞是'累計'。順便說一句,6000毫秒是6秒,所以你需要6分鐘360000例如(愚蠢,我知道但也許這就是爲什麼你沒有得到所需的效果)。

因此,我的猜測是,當您將 調用設置爲2時,您的程序會產生'報告';僅僅是因爲它有足夠的時間來完成(即 執行2次調用,或者如果您更喜歡在6秒鐘內的 秒內調用它兩次)。當你將它設置爲5時,它只是沒有足夠的時間來完成! :)

爲了解決是否嘗試timeOut參數或者如果您更喜歡invocationTimeOut然後將其設置爲更高,以便您的測試可以在所需的時間範圍內完成。從你的成功案例設置來看invocationTimeOut = 20000就夠了;)

祝你好運!

+0

我試過了。如果我使用timeOut而不是invocationTimeOut,那麼即使我設置了_lastTimeOnly = true_,每次調用都會觸發after方法。 –

+0

好的,根據您的反饋更新我的回答@Chip李。你是否也嘗試增加實際的invocationTimeout = 20000? –

+0

是的,我做到了。我將invocationTimeout減少到6000來重現問題。 –

相關問題