Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 15, 2016

TestNG Time-Out Test





While running tests there is a chance of where certain tests get stuck or may take much more time than expected. In such a cases you may need to mark the said test case as fail and then continue. TestNG allows user to configure a time period to wait for a test to completely execute. For this we will use Time-out Test annotation property.

This can be configured in three ways:

1.At Suite Level: This will be applicable for all the tests in the TestNG test suite
2.At Method Level: This will be applicable for the said test method.
3.Method Overrides Suite Level : Method level override the time period if configured at the suite level.

Example program for the Suite Level:

import org.testng.annotations.Test;

public class TimeTestSuiteLevel
{
    @Test
    public void timeTestOne() throws InterruptedException
    {
        Thread.sleep(2000);
        System.out.println("Paused execution for 2000 MilliSeconds");
    }
    
    @Test
    public void timeTestTwo() throws  InterruptedException
    {
        Thread.sleep(4000);
        System.out.println("Paused execution for 4000 MilliSeconds");
    }
}

Will execute the above program using testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" time-out="3000">
  <test name="Test">
    <classes>
      <class name="TimeTestSuiteLevel"/>
    </classes>
  </test>
</suite>

Output for the above program is:

TimeSuiteLevel

In the above suite, we mentioned each test in the suite should execute within 3000 milliseconds. But coming to the methods; timeTestOne() is taking 2000 milliseconds to execute and timeTestTwo() is taking 4000 milliseconds to execute. So, the first method is passed and the second method is failed as it is taking more time than the mentioned time.

Example program for the Method Level:

import org.testng.annotations.Test;

public class TimeTestMethodLevel
{
    @Test(timeOut = 3000)
    public void timeTestOne() throws InterruptedException
    {
        Thread.sleep(2000);
        System.out.println("Paused execution for 2000 MilliSeconds");
    }
    
    @Test(timeOut = 3000)
    public void timeTestTwo() throws InterruptedException
    {
        Thread.sleep(4000);
        System.out.println("Paused execution for 4000 MilliSeconds");
    }
}





Execute the above program and will find the below result:

TimeMethodLevel

In the above example; timeTestOne() should execute within 3000 milliseconds but  the execution is taking 2000 milliseconds. So, the first method is passed  as the time-out for the test annotation is set to 3000 milliseconds and it is more than the execution time. But the second method timeTestTwo() should execute within 3000 milliseconds but it is taking 4000 milliseconds. So, the second method is failed as the time-out for the test annotation is set to 3000 milliseconds and it is less than the execution time.

Above both scenarios, functionality is same but the time-out is mentioned in different levels. In the first example the time-out is at suite level and it is applicable for all the methods in the suite. In the second example the time-out is method level.

Example program for the Method Overrides Suite Level:

import org.testng.annotations.Test;

public class MethodOverridesSuiteLevel
{
    @Test(timeOut = 1000)
    public void timeTestOne() throws InterruptedException
    {
        Thread.sleep(2500);
        System.out.println("Paused execution for 2.5 Seconds");
    }
    
    @Test
    public void timeTestTwo() throws InterruptedException
    {
        Thread.sleep(2500);
        System.out.println("Paused execution for 2.5 Seconds");
    }
}

Execute the above program using testng.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" time-out="3000">
  <test name="Test">
    <classes>
      <class name="MethodOverridesSuiteLevel"/>
    </classes>
  </test>
</suite>

Output for the above program is :

MethodOverridesSuiteLevel

In the above program, as per the configuration file the both tests should pass as the time-out in the configuration file is 3000 milliseconds and the programs pause time is less than this time. But the first test failed. The reason is; the time-out mentioned in the suite level is overridden by the time-out property of the test annotation at the method level. So, the first test failed.

This way we can handle the test methods if they are taking more execution time than the expected time.



Share this post: on Twitter on Facebook

TestNG Exception Test Parameterization using testng.xml file

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025