VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/what-do-you-mean-by-timeout-in-testng/

⇱ What do you mean by timeOut in TestNG? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What do you mean by timeOut in TestNG?

Last Updated : 23 Jul, 2025

In software testing, different test stages or areas may take too long more than usually expected and may at times lead to wastage of time and resources. There are some situations in testing that may call for imposing time limits within which a test must be completed; to this end, TestNG has a property called timeout. This allows setting the upper limit through which the test is allowed to be run. If this test goes beyond this limit, it gets failure status. This way, long tests or tests that are stuck will not hold the testing process.

Explaining timeOut in TestNG with Code Examples

Within TestNG, timeouts can be configured at a method level using the @Test annotation or configured globally by including the time-out attribute in the testng.xml file. After the specified duration, a test is still running, but TestNG has no choice but to stop the test and label it as failure.

Example 1: Using timeOut in the @Test Annotation

Here’s a basic example that shows how to set a timeout for individual test methods:

Explanation:

  • The @Test(timeOut = 2000) specified that a test should be done within the time, a particular time could not exceed 2000 milliseconds or 2 seconds.
  • The testMethod() function in the script shows a delay of 3000 milliseconds using Thread.sleep(3000). This being a higher value than the two second limit set on the test. so the test will fail.

Expected Output:

πŸ‘ output
Output

In this case, the test will throw an exception which is a timeout error. Hence, it can be conclude that the given method did not complete within the time frame of 2000 milliseconds and hence TestNG gives an exception called ThreadTimeoutException.

Example 2: Setting a Global Timeout in TestNG.xml file.

You can also set a global timeout for all tests in the suite by configuring the time-out attribute in the testng.xml file.

Explanation

  • Explanation: The time-out attribute in the suite in normally there to limit the execution time on all test case performance farmers to 2000 millisecond.
  • Any method that exceeds 2 seconds will be forcibly stopped and marked as a failure.

Expected Output:

πŸ‘ Output
Output

Result of Running Suite

πŸ‘ output
Result of running suite

The result will be similar to the first example, but this time the timeout is enforced for every method in the suite. A global timeout is applied via the testng.xml file.

Conclusion

The Timeout feature in TestNG provides help in dealing with the long or the waiting motion without any logical end while writing the test artefacts. This functionality improves the test suite by encouraging proper time management practices and setting up each test to take the reasonable amount of time. Regardless of whether it is implemented on the method, class, or suite, this feature provides efficient test management and faster feedback loops.

Comment
Article Tags:

Explore