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 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
1: Timeout Test at Suite Level
First of all
create one Class as 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.
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.
Verbose Attribute lets you obtain clear
reports through IDE console. This attribute will be placed inside the
<Suite> tag of testng.xml as shown below:
<suite name="Suite" parallel="tests" verbose="2">
There are 10 levels of Verbose, starting from 1 to 10.
verbose="1" — verbose="10"
Verbose="10" gives more test details on console output whereas verbose="1" display less details.
No comments:
Post a Comment