HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG – Timeout Test Tutorial

TestNG – Timeout Test Tutorial

While executing tests, there can be cases where certain tests get stuck or may take longer execution time than expected. In such scenarios, we may need to mark the said test as fail and then move to the next test in the suite.

In this tutorial, we will learn to configure TestNG tests to timeout after some pre-configured duration.

1. Timeout Configuration

To specify timeout duration, use timeOut attribute of @Test annotation.

@Test ( timeOut = 500 )

TestNG allows user to configure a time period to wait for a test to completely execute. Timeout can be configured at two levels:

  • Suite level – This will be applicable for all the tests in the said TestNG test suite
  • Test level – This will be applicable for the said test method and will override the timeout period if configured at the suite level

Let’s create a sample test and learn how timeout works in TestNG.

2. Test Timeout at Suite Level

In below test, we have two test methods i.e. timeTestOne() and timeTestTwo(). timeTestOne() will take 1000ms to execute completely whereas timeTestTwo() will take 400ms to execute completely.

We have enforced the execution time using Thread.sleep() method.

public class TimeoutSuite 
{
	@Test
	public void timeTestOne() throws InterruptedException {
		Thread.sleep(1000);
		System.out.println("Time test method one");
	}

	@Test
	public void timeTestTwo() throws InterruptedException {
		Thread.sleep(400);
		System.out.println("Time test method two");
	}
}

Now add a testng.xml file to the project root and put the following code to it. This code defines timeout period to 500ms.

<suite name="Time test Suite" time-out="500" verbose="1" >
  <test name="Timeout Test" >
    <classes>
      <class name="com.howtodoinjava.test.TimeoutSuite" />
    </classes>
  </test>
</suite>

Now run above tests using testng.xml. Output of above test run is given below:

[TestNG] Running: C:\somepath\TestNGExamples\testng.xml

Time test method two

===============================================
Time test Suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

As you can see from the test results, only timeTestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. timeTestOne() execution got cancelled because it took more time to complete than timeout duration configured.

Let’s now go ahead and learn to set the timeout at a test method level.

3. Test Timeout at Test Level

As mentioned earlier, you can specify the timeout at individual test level as well. This will give you flexibility to give appropriate time to run specific to each individual test method.

public class TimeoutMethod 
{
	@Test(timeOut = 500)
	public void timeTestOne() throws InterruptedException {
		Thread.sleep(1000);
		System.out.println("Time test method one");
	}

	@Test(timeOut = 500)
	public void timeTestTwo() throws InterruptedException {
		Thread.sleep(400);
		System.out.println("Time test method two");
	}
}

Output of above test run is given below:

[[TestNG] Running: C:\Users\somepath\testng-customsuite.xml

Time test method two
PASSED: timeTestTwo
FAILED: timeTestOne

org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.timeTestOne() didn't finish within the time-out 500

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

In above test methods timeTestOne() failed because it was not completely executed within timeout period specified.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Mike

    June 18, 2019

    Note: The timeout at suite level (that you can set in the testng xml file) is not a timeout for each individual test, but a timeout for the whole suite (if you set the timeout to 10 minutes, then the suite will exit after 10 minutes, no matter if there are still tests left or not)

    Source: https://github.com/cbeust/testng/issues/2092

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces