Timeout in TestNG: How to Automatically Stop Running a Test Case if it Gets Stuck

Print View Mobile View

In TestNG, we can automatically stop executing a test if it gets stuck or takes longer than expected time to complete. This functionality is enabled using the “timeOut” attribute of @Test annotation.

@Test ( timeOut = 1000 ) //Time in milliseconds

Timeout can be configured at test method or suite level. Let’s have a look at both the cases.

Timeout Demo at Method Level

In below example, we have two test methods: PassTest() and FailTest(). We are using Thread.sleep() method to control their execution time.

File: TimeoutDemo.java

package TestNGDemo;

import org.testng.annotations.Test;

public class TimeoutDemo {
	@Test(timeOut = 2000) // time in milliseconds
	public void PassTest() throws InterruptedException {
		Thread.sleep(1000);
		System.out.println("This test was successful");
	}

	@Test(timeOut = 4000)
	public void FailTest() throws InterruptedException {
		Thread.sleep(5000);
		System.out.println("This test fails");
	}
}

PassTest() will take 1000ms to execute and we have set “timeOut” at 2000ms at suite level, so this should Pass. FailTest() will take 5000ms to execute completely, but as we have set “timeOut” at 4000ms, this test would fail.
TestNG Timeout

Timeout Demo at Suite Level with TestNG XML

Now, we will have a look at setting “time-out” at Suite level and overriding it at test method level using TestNG XML file.

Again, there are two methods in this class: PassTest() and FailTest(). They would run for 3000ms.

File: TimeoutDemo.java

package TestNGDemo;

import org.testng.annotations.Test;

public class TimeoutDemo {
	@Test
	public void PassTest() throws InterruptedException {
		Thread.sleep(3000);
		System.out.print("This test should pass");
	}

	@Test
	public void FailTest() throws InterruptedException {
		Thread.sleep(3000);
		System.out.println("This test should fail");
	}
}

In TestNG XML file, we are going to set “time-out” at 2000ms. This would ideally fail both the tests.

File: testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Timeout Suite" time-out="2000">
	<test name="Pass Demo" time-out="4000">
		<classes>
			<class name="TestNGDemo.TimeoutDemo">
				<methods>
					<include name="PassTest" />
				</methods>
			</class>
		</classes>
	</test>
	<test name="Fail Demo">
		<classes>
			<class name="TestNGDemo.TimeoutDemo">
				<methods>
					<include name="FailTest" />
				</methods>
			</class>
		</classes>
	</test>
</suite>

To prevent the PassTest() from failing, we will change its “time-out” value to 4000ms. This would override the value from suite level.

Now if you rung this script, output for this suite would be:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
This test should pass
===============================================
Timeout Suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

The second method will fail as it’s execution is not completed in the specified timeout period.