TestNG – Timeout Test

In this tutorial, we will show you how to perform timeout test in TestNG. The “timeout” means if a unit test is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed.

This “timeout” can also use for performance test, to ensure the method is returned within a reasonable time.

TestTimeout.java

package com.mkyong.testng.examples.timeout;

import org.testng.annotations.Test;

public class TestTimeout {

	@Test(timeOut = 5000) // time in mulliseconds
	public void testThisShouldPass() throws InterruptedException {
		Thread.sleep(4000);
	}

	@Test(timeOut = 1000)
	public void testThisShouldFail() {
		while (true);
	}

}

Output


[TestNG] Running:

PASSED: testThisShouldPass
FAILED: testThisShouldFail
org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.testThisShouldFail() didn't finish within the time-out 1000
	at com.mkyong.testng.examples.timeout.TestTimeout.testThisShouldFail(TestTimeout.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:46)
	at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:37)
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
NoahVipin C MohanNuongNguyenTestNG Tutorials | J2EE Web Development Tutorials Recent comment authors
newest oldest most voted
Vipin C Mohan
Guest
Vipin C Mohan

Hi,
Thanks for the post.
I’m wondering is there any way to add the timeouts at project/package/class level. (Say fail any tests is taking more than a minute)
I have so many tests and it is tough to add timeout to each and every tests.

Noah
Guest
Noah

Hi,
Is it possible to pass the timeout as a parameter from the testng.xml suite? The reason I need it is that I have hundreds of test that I want to change their timeout.

NuongNguyen
Guest
NuongNguyen

Thanks for post, can you tell me, how long time to set timeout of thread i should set up?

trackback
TestNG Tutorials | J2EE Web Development Tutorials

[…] Tutorial 4 – Time Test Make sure the testing method will return after certain time. […]