Timeout in TestNG

In this section we will discuss on Timeout in TestNG. If  your test takes longer than the expected time, then test will be terminated and logged as failed test in testing-failed.xml. This is called “Timeout”

One question may come to your mind that in which scenario your test will be taking longer time and how you can overcome this problem

Below scenario may take longer time:

  • Sometime page taking longer time to load because of the slow network.
  • If test method not running  properly, then test will take longer time to execute. Sometime certain test method gets stuck or may take longer time to execute than expectation.
  • If one test is dependent with another test  and it will not get the expected input from the dependent test, this may also run longer time than expectation.

To overcome above problem, we need to specify Timeout  with @Test annotation and proceed to execute further testcase. When we define @Test(timeOut=10000) then this will wait for 10000 millisecond. If method execution will not be completed in that 10000 millisecond then this will skip the current test and continue the next testcase.

Both JUnit and TestNG supports this features.

Let’s understand this with an Example

Example:

import org.testng.annotations.Test;
public class TimeoutExample {
	@Test
	public void TestCase1(){
			System.out.println("Testcase ready to execute");
		}
  
	@Test(timeOut=10000) // specify time in milliseconds

	public void TestcaseTimeOut () throws InterruptedException{
		Thread.sleep(30000);
              
	}
}

If we will execute the above code the we will get output as below. Though TestcaseTimeOut method is waiting for 30000 and timeout mentioned as 10000, TestcaseTimeOut is marked as failed. If we will make Thread.sleep(30000); in TestcaseTimeOut then this will be marked as Passed.

Output:

Test Method : TestCase1 – PASSED

Test Method : TestcaseTimeOut – FAILED

In the above example, we have set the TimeOut as 30000 ms i.e. 30 sec. so the test will wait for 30 sec before exception triggered.

Now we have clear picture on what is Timeout in TestNG.

Leave a Reply

Your email address will not be published. Required fields are marked *