PAGE-12 : TESTNG TIMEOUT

TestNG – Timeout Test 
While running tests there can be cases where certain tests get stuck or may take much more time than expected. In such a case you may need to mark the said test case as fail and then continue. In this tutorial, we will learn to configure TestNG tests to timeout after some configured time duration.
TestNG allows user to configure a time period to wait for a test to completely execute. Timeout can be configured in two ways:

·         At suite level: This will be applicable for all the tests in the said TestNG test suite
·         At each test method level: This will be applicable for the said test method and will override the time period if configured at the suite level

To specify timeout duration, use “timeOut” attribute of @Test annotation.
@Test ( timeOut = 500 )
Let’s create a sample test and learn how timeout works in TestNG.

Example of Timeout Test at Suite Level

In below test, we have two test methods i.e. timeTestOne() and timeTestTwo(). timeTestOne() will take 1000ms to execute completely whereas timeTestTwo() will take 400ms to execute completely. We have enforced the execution time using Thread.sleep() method.

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");
    }
}
Now add a testng.xml file to the project root and put the following code to it. This code defines timeout period to 500ms.
<suite name="Time test Suite" time-out="500" verbose="1" >
  <test name="Timeout Test" >
    <classes>
      <class name="com.howtodoinjava.test.TimeoutSuite" />
    </classes>
  </test>
</suite>

Now run above tests using testng.xml. Output of above test run is given below:
[TestNG] Running: C:\somepath\TestNGExamples\testng.xml
 Time test method two

===============================================
Time test Suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================
As you can see from the test results, only timeTestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. timeTestOne() execution got cancelled because it took more time to complete than timeout duration configured.
Let’s now go ahead and learn to set the timeout at a test method level.

Example of Timeout Test at Method Level

As mentioned earlier, you can specify the timeout at method level as well. This will give you flexibility to give appropriate time to run specific to each individual test method.
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");
    }
}


Output of above test run is given below:
[[TestNG] Running: C:\Users\somepath\testng-customsuite.xml
 Time test method two
PASSED: timeTestTwo
FAILED: timeTestOne

org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.time
TestOne() didn't finish within the time-out 500

===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
In above test methods timeTestOne() failed because it was not completely executed
within timeout period specified.


No comments:

Post a Comment

About Me

My photo
You can reach me out at : jimmiamrit@gmail.com

Total Pageviews