使用invocationTimeOut和invocationCount时不执行@AfterMethod方法> 2(TestNG 6.9 - > 6.10)

时间:2017-01-13 03:38:43

标签: java testng

我希望我的测试重试5次,不超过x分钟。在最后一次调用之后,它将在@Aftermethod中调用mergeReport方法。这是我的示例代码:

@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

有没有人遇到过这个问题?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用timeOut而不是invocationTimeout?

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

来自testng doc:

  

invocationTimeOut - >此测试的最大毫秒数   应该采用所有调用计数的 累积 时间。这个   如果未指定invocationCount,则将忽略该属性。

     

timeOut - >此测试应采用的最大毫秒数。

我认为你是在追求你想要的东西之后!

然后,再次提及您的解释,您想要运行X分钟。关键词是'累积' 。顺便说一句,6000毫秒是6秒,所以你需要360000分钟6分钟(傻,我知道,但也许这就是为什么你没有达到预期的效果)。

  

因此,我的猜测是您的计划会产生报告'当你设置   调用2;只是因为它有足够的时间来完成(即   执行2次调用,如果您愿意,请在6次调用中调用两次   秒边距)。当你将它设置为5时,它就没有足够的   时间来完成! :)

为了解决是否尝试使用timeOut参数或者您更喜欢invocationTimeOut,然后将其设置为更高的值,以便您的测试可以在所需的时间范围内完成。从您成功的示例设置判断 invocationTimeOut = 20000 应该足够了;)

祝你好运!