testng timeout test

By | May 9, 2020

TestNG timeOut method can be used in below real time scenarios.

1. When a loop goes infinite, test never ends. In this case we can set a predefined time limit using Timeout() method.
2. When a response is slow (slow page loading), it will take more time than usual. In this case we can use Timeout() and can continue with next steps in the code.
3. When a test method get struck.

Syntax: @Test(timeOut=5000)
The maximum number of milliseconds this test should wait for. If it hasn’t returned after this time, it will be marked as FAIL.

Timeout() method is available in both TestNG and JUnit 4.

We can configure Timeout() method at suite level and method level.

Suite level:

xml:

Output:

[TestNG] Running:
C:\Users\name\AppData\Local\Temp\testng-eclipse–1796236022\testng-customsuite.xml

Method3
PASSED: method3
FAILED: method1
org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.method1() didn’t finish within the time-out 3000
FAILED: method2
org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.method2() didn’t finish within the time-out 3000

===============================================
Suite
Total tests run: 3, Failures: 2, Skips: 0
===============================================

Method level:

Output:
[TestNG] Running:
C:\Users\name\AppData\Local\Temp\testng-eclipse–130385487\testng-customsuite.xml

FAILED: MethodLevel_1
org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.MethodLevel_1() didn’t finish within the time-out 1000
at java.lang.Thread.exit(Thread.java:758)

FAILED: MethodLevel_2
org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.MethodLevel_2() didn’t finish within the time-out 1000

===============================================
Default test
Tests run: 2, Failures: 2, Skips: 0
===============================================

 

Recommended Posts:

String split function

Get last 4 chars from String

Covert String to int in Java

Hashset and LinkedHashset differences in java

SQL quick reference for interview preparation

Selenium tutorials

 

Tags: TestNG timeout, timeouts in testng

Leave a Reply

Your email address will not be published. Required fields are marked *