Timeout Attributes In TestNG: When we are running an automation script, some scripts take a longer time to execution then expected. So in those cases, we need to mark such cases as fail and then continue. So in this post, we are going to see how we can mark fail such test cases, which are taking a long time to execute with the help of testNG time out.
TestNG allows us to achieve to do in 2 ways:
- Suite Level: If you define at suite level, then that is applicable for all the test methods in that TestNG suite.
- Test Method Level: If you define at the method level, then the timeout duration will apply to that method only. If previously suite level is declared, and later you specify at the method level, in that case, it will override the time mentioned at the suite level.
Time timeOut attribute within the @Test annotation method is assigned a value specifying the number of milliseconds. In case 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 below class, we have two methods, where in the first test method, we have put the wait time 1000ms, and in the second test method, the wait time is 400ms. But in the suite file, we have mentioned 500ms, so the first method will be failed because here we have put 1000ms, which is higher than the timeout millisecond.
TestNg Class:
public class TimeoutSuite { @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"); } }
TestNg.XML
<suite name="Time test Suite" time-out="500" verbose="1"> <test name="Timeout Test"> <classes> <class name="com.howtodoinjava.test.TimeoutSuite" /> </classes> </test> </suite>
Let us go to another way, which is using method level:
Timeout Attribute at Method Level
TestNg.class:
public class TimeoutMethod { @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"); } }
Use the same testng.xml file to run the above TestNG class, and here we have mentioned the timeout duration is 500 for each test method and as the first test method takes more than the mentioned time, thats why that will fail.
Source: Link
Leave a Reply