Timeouts in TestNG

Timeout in TestNG – Fail Tests in case of Timeout

Last updated on

The automation test suites have the tendency to take too much time in case the elements are not readily available for interaction. Also, in certain tests, we might have to wait for some asynchronous event to occur in order to proceed with the test execution.

In these cases, we may want to limit the test execution time by specifying an upper limit of timeout exceeding which the test method is marked as a failure. TestNG provides us timeOut attributes for handling these requirements.

TimeOut in TestNG

Time timeOut attribute within the @Test annotation method is assigned a value specifying the number of milliseconds. In case the test method exceeds the timeout value, the test method is marked as failure with ThreadTimeoutException.

@Test(timeOut = 1000)

Code Snippet

In the below code snippet we have specified a timeout of 1000ms. Inside the test methods we can see that a Thread.sleep() of 3seconds is introduced. On test execution, we can notice in the output that the test fails with ThreadTimeoutException as the timeout is 1 second and the test takes little over 3 seconds to execute.

@Test(timeOut = 1000)
public void timeOutTest() throws InterruptedException {
   //Test logic


FAILED: timeOutTest
Method org.testng.internal.TestNGMethod.timeOutTest() 
didn't finish within the time-out 1000

PS: The test stops execution as soon as the timeout duration is reached, marking the test as a failure.

Leave a Comment