What is invocationTimeOut in TestNG?
Overview
In this tutorial, let’s understand invocationTimeOut test method attribute in TestNG. In TestNG we can specify the number of times the method can be invoked using the invocationCount attribute. This attribute is the number of times the test method would be invoked during the run.
invocationTimeOut
When we set invocationCount and invocationTimeOut on a test method, invocationTimeOut is the maximum time period TestNG will wait for all the invocations of the test method specified in the attribute invocationCount.
This attribute specified is in milliseconds of time. For example, to specify 6 secs we have to use
invocationTimeOut=6000
Let’s see a simple example. The test method executes 5 times.( invocationCount=5)
Sample Code Listing
package com.testingdocs.testng.tutorials; import org.testng.annotations.Test; //www.TestingDocs.com - TestNG Tutorials public class TestNGInvocationCount { @Test(invocationCount=5,invocationTimeOut=6000) public void sampleTest() throws Exception{ System.out.println("Foo."); Thread.sleep(1000); } }
Output
Foo.
Foo.
Foo.
Foo.
Foo.
PASSED: sampleTest
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
The invocationTimeOut specified in the Test is 6000 ms. The test passed because all the test method invocations executed before the invocationTimeOut.
The method waits for 1000 ms.
invocationTimeOut > ~ 5 * 1000 ms
Since 6000 > 5*1000 +(small delta ) the test is marked as pass.
timeOut vs invocationTimeOut
Test annotation has another attribute called timeOut. This attribute is the maximum time the method should take to complete. If we mix both the attributes the timeOut will override the invocationTimeOut value.
package com.testingdocs.testng.tutorials; import org.testng.annotations.Test; //www.TestingDocs.com - TestNG Tutorials public class TestNGInvocationCount { @Test(invocationCount=5,invocationTimeOut=6000,timeOut=900) public void sampleTest() throws Exception{ System.out.println("Foo."); Thread.sleep(1000); } }
The test would be marked as failure as the timeOut attribute overrides the invocationTimeOut value. i.e When both the values are specified the test run time should be less both the values for the test to be marked as pass.
Test runs that exceed the timeout will fail with the ThreadTimeoutException:
org.testng.internal.thread.ThreadTimeoutException: Method com.testingdocs.testng.tutorials.TestNGInvocationCount.sampleTest() didn't finish within the time-out 900
–
TestNG Tutorials on this website can be found at:
https://www.testingdocs.com/testng-framework-tutorial/
For more details on the TestNG Framework, visit the official website of TestNG at: