TestNG Timeout Attributes Of @Test Annotation

Timeout Attributes In TestNG: Some scripts take longer to execute than expected when running an automation script. In those cases, we need to mark such test cases as fail and then continue.

In this post, we will see how we can mark failed test cases that take a long time to execute with the help of the testNG timeOut attribute.

TestNG allows us to achieve in 2 ways:

  • Suite Level: If you define at suite level, that applies to all the test methods in that TestNG suite.
  • Test Method Level: If you define the method level, the timeout duration will only apply to that method. If the previous suite level is declared and you later specify it at the method level, it will override the time mentioned at the suite level.

The timeout attribute within the @Test annotation method is assigned a value specifying the number of milliseconds. If the test method exceeds the timeout value, the test method is marked as a failure with ThreadTimeoutException.

How to Define Timeout Attributes at Suite Level

In the class below, we have two methods; in the first test method, we have set the wait time to 1000ms; in the second test method, the wait time is 400ms.

But in the suite file, we have mentioned 500ms, so the first method will fail because here we have put 1000ms, which is higher than the timeout millisecond.

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class TimeoutSuiteLevel 
{
	@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, let us create a test suite for the above class, and here is the test suite testng.xml file of the above class.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" time-out="500" verbose="1">
  <test thread-count="5" name="Test">
    <classes>
      <class name="com.softwaretestingo.testng.attributes.TimeoutSuiteLevel"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

When we run the above XML file, the output will be

[RemoteTestNG] detected TestNG version 7.8.0
Time test method two

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

Let us go to another way, where we will try to apply the TimeOut attribute at the method level:

Timeout Attribute at Method Level

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class TimeoutMethodLevel 
{
	@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");
	}
}

When we run the above class, the output will be

[RemoteTestNG] detected TestNG version 7.8.0
Time test method two
PASSED: com.softwaretestingo.testng.attributes.TimeoutMethodLevel.timeTestTwo
FAILED: com.softwaretestingo.testng.attributes.TimeoutMethodLevel.timeTestOne
org.testng.internal.thread.ThreadTimeoutException: Method
===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment