【问题标题】:@AfterMethod method not executed when using invocationTimeOut and invocationCount > 2 (TestNG 6.9 -> 6.10)使用 invocationTimeOut 和 invocationCount > 2 (TestNG 6.9 -> 6.10) 时未执行 @AfterMethod 方法
【发布时间】:2017-01-13 03:38:42
【问题描述】:

我希望我的测试重试 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

有人遇到过这个问题吗?

【问题讨论】:

  • 我不确定 325 是不是类似的问题。你能打开一个新的并在那里提供一个完整的可运行项目吗?
  • 我通过使用 TestListenerAdapter 和 IInvokedMethodListener 解决了这个问题。如果在达到 invocationCount = 5 之前测试超时,我的自定义侦听器会将其设置为 5,并且将触发 after 方法。 while (iTestResult.getMethod().getCurrentInvocationCount() < iTestResult.getMethod().getTotalInvocationCount()) { iInvokedMethod.getTestMethod().incrementCurrentInvocationCount(); }

标签: java testng


【解决方案1】:

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

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

来自测试文档:

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

timeOut --> 这个测试应该花费的最大毫秒数。

我想你已经超时了!

然后,您在说明中提到您想运行 X 分钟。而关键词是'累积'。顺便说一句,6000 毫秒是 6 秒,所以你需要 360000 6 分钟(我知道这很傻,但也许这就是你没有得到想要的效果的原因)。

因此,我猜你的程序会在你设置时生成“报告” 调用2;只是因为它有足够的时间来完成(即 执行 2 次调用,或者如果您愿意在 6 次中调用它两次 秒余量)。当您将其设置为 5 时,它就不够了 是时候完成了! :)

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

祝你好运!

【讨论】:

  • 我试过了。如果我使用 timeOut 而不是 invocationTimeOut,即使我设置了 lastTimeOnly = true,每次调用都会触发 after 方法。
  • 好的,根据您的反馈@Chip Li 更新了我的答案。您是否也尝试过增加实际的 invocationTimeout=20000?
  • 是的,我做到了。我将 invocationTimeout 减少到 6000 以重现该问题。
  • 所以我想增加调用次数解决了你的问题(或者不是?)我很困惑 :)
  • 我通过使用 TestListenerAdapter 和 IInvokedMethodListener 解决了这个问题。如果在达到 invocationCount = 5 之前测试超时,我的自定义侦听器会将其设置为 5,并且将触发 after 方法。 while (iTestResult.getMethod().getCurrentInvocationCount()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
相关资源
最近更新 更多