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:
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:
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 :
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.