Timeout Test in TestNG

hop over to these guys Timeout Test in TestNG:

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  http://ligaspanyol.net/?mikroskop=site-de-rencontre-avec-gps&7a1=fa configure TestNG tests to timeout after some configured time duration.

TestNG allows user to  http://heartpearls.com/?mistyu=hoteles-para-solteros-en-huatulco&89f=25 configure a time period to wait for a test to completely execute. Timeout can be configured in two ways:

  • important source At suite level:This will be applicable for all the tests in the said TestNG test suite
  • opcje binarne jaka strategia 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 “ http://meliggoi.gr/mokryxa/385 timeOut” attribute of @Test annotation.

citas imss por internet leon gto @Test (timeOut = 500)

Let’s create a sample test and learn how timeout works in TestNG.

http://sebastiancorreal.com/?molekyla=best-online-dating-sites-in-africa&bc0=89 Example 1: Timeout Test at Suite Level

First of all create one Class as marco island dating site TimeoutTest.Java as below.

package Timeout;

import org.testng.annotations.Test;

public class TimeoutTest {
  @Test
    public void TestOne() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("===TimeOut test method One Executed===");
    }
 
    @Test
    public void TestTwo() throws InterruptedException {
        Thread.sleep(400);
        System.out.println("===TimeOut test method Two Executed===");
    }
}

In above class, we have two test methods i.e. TestOne() and TestTwo(). 

TestOne() will take 1000ms to execute completely whereas TestTwo() will take 400ms to execute completely. We have enforced the execution time using Thread.sleep() method.

Now configure the testng.xml file as below. This code defines timeout period to 500ms.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TimeOut test Suite" time-out="500" verbose="1" >
  <test name="Timeout Test">
    <classes>
   	 <class name="Timeout.TimeoutTest"/>
     </classes>
  </test>
</suite>

Now run above tests using testng.xml. Output of above test run is given below:

[TestNG] Running:
  D:\WorkSpace\DemoTestNG\testng.xml

===TimeOut test method Two Executed===

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

As you can see from the test results, only TestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. TestOne() execution got cancelled because it took more time to complete than timeout duration configured.

conocer gente nueva por pin Example 2: Timeout Test at Method Level

In the previous example we are defining the time at Suite Level in testng.xml file. But in method level Time Out Test you can define the time using “ timeOut” attribute of @Test annotation as below.

TimeoutTest.java

package Timeout;

import org.testng.annotations.Test;

public class TimeoutTest {
  @Test(timeOut = 500)
    public void TestOne() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("===TimeOut test method One Executed===");
    }
 
    @Test(timeOut = 500)
    public void TestTwo() throws InterruptedException {
        Thread.sleep(400);
        System.out.println("===TimeOut test method Two Executed===");
    }
}

Output of above test run is given below:

[TestNG] Running:
  C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse-817432314\testng-customsuite.xml

===TimeOut test method Two Executed===

[Utils] Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default test.xml
[Utils]   Directory D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true
PASSED: TestTwo
FAILED: TestOne

In above test methods TestOne() failed because it was not completely executed within timeout period specified.