问题描述:

I want my test retry 5 times and not longer than x minutes. After the last invocation, it will invoke mergeReport method in @Aftermethod. Here my example code:

@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");

}

Test result will be empty. But when I change invocationCount to 2, the test result will be:

Report

I search through the internet and found a similar issue from 2014 but it still have no answer: https://github.com/cbeust/testng/issues/325

Has anyone met this issue?

网友答案:

Have you tried with timeOut instead of invocationTimeout?

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

From testng doc:

invocationTimeOut--> The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.

timeOut --> The maximum number of milliseconds this test should take.

I think you are after timeOut for what you want!

Then again, you mention at your decription that you want to run for a X number of minutes. And the key word is 'cumulated '. By the way 6000 milliseconds is 6 seconds, so you need 360000 for 6 minutes for example (silly, I know but maybe that is why you don't get the desired effect).

Hence, my guess will be that your program spawns 'Report' when you set invocation to 2; only because it has enough time to complete (i.e. perform 2 invocations, or call it twice if you prefer within the 6 seconds margin). When you set it to 5 it just does not have enough time to complete! :)

In order to fix whether try the timeOut parameter or if you prefer invocationTimeOut then set it to something higher so your test can complete within the desired time-frame. Judging from your successful example setting invocationTimeOut =20000 should suffice ;)

Best of luck!

相关阅读:
Top