Site icon TestingDocs.com

How to Test method Timeouts in TestNG

Overview

In this tutorial, we will learn how to Test method Timeouts in TestNG framework. We can use this feature to automatically mark the @Test methods that take longer execution time as failures. We can specify the timeout in milliseconds, using the timeOut attribute of @Test annotation.

A long executing test would slog the other tests. In such cases we can add the timeOut attribute to the test methods.

timeOut attribute

Sample usage of the timeOut attribute. For example to set the timeout of 5 secs to a test method:

@Test(timeOut = 5000) // SLA – 5secs

public void testMethodWithFiveSecSLA()

 

Sample Code

 

package com.testingdocs.tests.timeouts; 

import org.testng.annotations.Test; 

//################################### 
// Testing timeout in Test methods 
// www.TestingDocs.com
//################################### 

public class TimeoutsTestNG { 

@Test(timeOut = 5000) // SLA - 5secs Time in milliseconds 
public void testMethodShouldPass() throws InterruptedException { 
         Thread.sleep(4000); 
} 

@Test(timeOut = 5000) 
public void testMethodShouldFail() { 
         while (true);// indefinite loop - this test will fail
 } 
}

 

Timeout in XML file

Notice that for every Test method we are specifying the timeOut attribute. To set the timeout for multiple tests we can set this in the TestNG XML file.

We can specify the timeout at various level in the XML file.

Let’s see how we can specify the timeout in the XML file.

 

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
  
<!-- TestNG Tutorials -->
<!-- www.TestingDocs.com --> 
<suite name="Timeout Tests" time-out="5000"> 
  <test name="SampleTest1"> 
	<classes>
	  <class name="com.testingdocs.tests.timeouts.TimeoutsSuiteXMLDemo" />
	</classes> 
  </test> 
</suite>

 

 

We can remove the timeout attributes from the @Test annotation in the test methods. We can place all the test classes that test with the timeout = 5 secs under this suite.

Test methods without timeOut annotation attribute. Timeout for both the methods would be set to 5 secs as specified in the XML file.

 

 

package com.testingdocs.tests.timeouts;

import org.testng.annotations.Test;

//###################################
//# Testing timeout in Test methods
//# www.TestingDocs.com
//###################################

public class TimeoutsSuiteXMLDemo {
	// 5 secs timeout specified in the testng.xml file
	@Test 
	public void testMethodShouldPass() throws InterruptedException {
		Thread.sleep(4000);
	}

	@Test
	public void testMethodShouldFail() {
		while (true);// indefinite loop - this test will fail
	}

}

 

 

 

 

Use of Timeouts

For example, assume we are testing an API call with SLA( Service level agreement) that the API should respond within 5 secs of time from the API request is sent. It is considered a failure if the API responds after 5 sec as per the SLA. We can use the timeout for the service tests to check the slow API calls.

Points to Ponder

1.What happens if we specify time-out as 5000 ms in <suite> XML file and @Test(timeOut=7000ms) in the test method. The test method takes 6000 ms to complete execution.

2.

 

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<!-- TestNG Tutorials -->
<!-- www.TestingDocs.com --> 
<suite name="Timeout Tests" time-out="5000"> 
  <test name="SampleTest1" time-out="3000"> 
	<classes>
	  <class name="com.testingdocs.tests.timeouts.TimeoutsSuiteXMLDemo" />
	</classes> 
  </test> 
</suite>

 

Given the Test method:

@Test 
public void testMethod() throws InterruptedException {
Thread.sleep(4000);
}

What is the test result Pass/Fail?

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:

https://testng.org

Exit mobile version