Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

What does the test timeOut mean in TestNG?

The timeOut attribute in TestNG sets the maximum time, in milliseconds, that a test method is allowed to run. If the method takes longer than that limit, TestNG interrupts it, marks the test as failed, and reports a ThreadTimeoutException. It prevents slow, hung, or infinite-loop tests from blocking the rest of your suite.

For example, @Test(timeOut = 5000) allows the method five seconds; if it runs past that, TestNG terminates it. You can apply timeouts at the method level through the @Test annotation or globally through the testng.xml suite file.

Understanding the timeOut Attribute in TestNG

TestNG runs each timed method in a separate worker thread and watches how long it takes. When the elapsed time crosses the configured timeOut value, TestNG interrupts that thread and records the result as a failure rather than allowing the method to hang indefinitely. This behavior is essential in continuous integration pipelines, where a single stuck test could otherwise freeze an entire build.

The value is always expressed in milliseconds, so 1000 equals one second. A timeout is useful for performance-sensitive assertions, API calls bound by a Service Level Agreement (SLA), and any operation that could loop forever. To learn how timeouts fit alongside other annotations, see the TestMu AI TestNG framework tutorial.

How to Set a Method-Level Timeout

The most common approach is to declare the timeout directly on the @Test annotation. This keeps the limit close to the method it governs and overrides any suite-wide default. In the example below, the method is expected to finish within two seconds, but the simulated three-second delay forces a timeout failure.

import org.testng.annotations.Test;

public class TimeoutTest {

    @Test(timeOut = 2000) // limit is 2000 ms (2 seconds)
    public void testWithTimeout() throws InterruptedException {
        // Simulates work that takes 3 seconds
        Thread.sleep(3000);
        // TestNG interrupts this method at 2000 ms and marks it failed
    }
}

When this runs, TestNG fails the method with org.testng.internal.thread.ThreadTimeoutException because it did not complete inside the allotted window.

How to Set a Suite-Level Timeout in testng.xml

To apply one default limit across many methods, add a time-out attribute to the <suite> or <test> tag in your testng.xml file. Any method that declares its own @Test timeOut will still override this value for itself.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RegressionSuite" time-out="5000">
  <test name="SmokeTests">
    <classes>
      <class name="com.example.TimeoutTest" />
    </classes>
  </test>
</suite>

Here every method under the suite inherits a 5000 ms limit unless it specifies otherwise. This is a clean way to enforce a baseline SLA across a large regression run.

timeOut vs invocationTimeOut

TestNG offers a second timing attribute for repeated executions. Knowing the difference prevents confusing failures:

  • timeOut: Caps the duration of a single invocation of the method. Best for ordinary functional or UI tests.
  • invocationTimeOut: Caps the combined duration of all invocations when a method runs multiple times through invocationCount. Best for repeated or load-style runs.
  • Combined use: Pair invocationTimeOut with invocationCount, for example @Test(invocationCount = 10, invocationTimeOut = 10000), to bound ten runs to ten seconds total.

Common Mistakes and Troubleshooting

  • Confusing seconds and milliseconds: Setting timeOut = 5 gives a method only five milliseconds, not five seconds. Always convert to milliseconds.
  • Timeouts too tight for UI tests: Selenium page loads vary. A limit that is too low produces flaky failures unrelated to real defects.
  • Ignoring parallel contention: When many threads share CPU, individual methods slow down. Add headroom so parallel runs do not trip the timeout.
  • Using timeOut instead of explicit waits: timeOut is a safety net, not a synchronization tool. Use WebDriver explicit waits to handle dynamic elements.
  • Expecting a graceful cleanup: When TestNG interrupts a method, teardown may be skipped. Keep external resources idempotent so an interrupted test does not leak state.

Running TestNG Timeout Tests Across Real Browsers and Devices

Timeout values behave differently across browsers, operating systems, and network conditions, so testing on a single local machine can hide false positives and negatives. With TestMu AI, you can run the same TestNG suite across 3000+ real browsers and operating systems on a scalable cloud Selenium automation grid, calibrating timeouts against realistic latency instead of a fast local loopback.

Running in parallel across the cloud also shortens overall suite time, which lets you keep sensible per-method limits without extending the total build. Explore how TestMu AI handles large TestNG suites through its cross browser testing platform.

Conclusion

The TestNG timeOut attribute is a simple but powerful guardrail: it defines, in milliseconds, how long a test method may run before TestNG fails it. Apply it at the method level for precise control, use testng.xml for suite-wide defaults, and reach for invocationTimeOut when a method repeats. Combined with explicit waits and a realistic cloud grid, timeouts keep your automation fast, deterministic, and free of hung builds.

Frequently Asked Questions

In what unit is the TestNG timeOut value defined?

The timeOut value is always defined in milliseconds. A value of 5000 means five seconds and 2000 means two seconds. TestNG has no separate unit for seconds, so you must convert your intended limit into milliseconds before assigning it to the timeOut attribute.

What exception does TestNG throw when a timeout is exceeded?

When a test method exceeds its timeOut limit, TestNG interrupts the thread and fails the test with org.testng.internal.thread.ThreadTimeoutException. The report shows the method as failed rather than skipped, along with the configured limit and the actual time taken.

Can I set a timeout for an entire TestNG suite?

Yes. Add a time-out attribute to the suite or test tag in testng.xml. This applies a default limit to every method under that scope. A timeOut declared on an individual @Test annotation overrides the suite-level value for that specific method.

What is the difference between timeOut and invocationTimeOut?

timeOut limits a single invocation of a test method. invocationTimeOut caps the combined time of all invocations when a method runs multiple times through invocationCount. Use invocationTimeOut together with invocationCount for repeated or load-style executions.

Does the TestNG timeOut work with parallel execution?

Yes. Each method still enforces its own timeOut when tests run in parallel threads. However, heavy parallelism can slow individual threads, so set timeouts with enough headroom to avoid false failures caused by shared CPU or network contention.

Should I use timeouts for Selenium WebDriver tests?

Yes, but choose realistic values. UI tests involve page loads and network calls, so a value that is too low causes flaky failures. Combine a generous TestNG timeOut with explicit waits inside the test for reliable, deterministic Selenium automation.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests