Thursday, 12 March 2020

TestNG: Specify timeout at suite level

'time-out' attribute is used to set timeout at suite level.

SuiteTimeOutTest.java
package com.sample.app.tests;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

public class SuiteTimeOutTest {

 @Test
 public void test1() {
  System.out.println("Going to sleep for 6 seconds");

  try {
   TimeUnit.SECONDS.sleep(6);
  } catch (InterruptedException e) {
   System.out.println("In catch block : " + e.getMessage());
  }

 }

 @Test
 public void test2() {
  System.out.println("Going to sleep for 4 seconds");

  try {
   TimeUnit.SECONDS.sleep(4);
  } catch (InterruptedException e) {
   System.out.println("In catch block : " + e.getMessage());
  }

 }

 @Test
 public void tes3() {
  System.out.println("Going to sleep for 2 seconds");

  try {
   TimeUnit.SECONDS.sleep(2);
  } catch (InterruptedException e) {
   System.out.println("In catch block : " + e.getMessage());
  }

 }
}

suiteTimeLimitDemo.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="sanity test suite" time-out="5000">

 <test name="Sanity test 1">
  <classes>
   <class name="com.sample.app.tests.SuiteTimeOutTest"></class>
  </classes>
 </test>


</suite>

How to run the test suit?
Right click on ‘suiteTimeLimitDemo.xml’ file, Run As -> TestNG Suite.


You will see below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
Going to sleep for 2 seconds
Going to sleep for 6 seconds
In catch block : sleep interrupted
Going to sleep for 4 seconds

===============================================
sanity test suite
Total tests run: 3, Passes: 2, Failures: 1, Skips: 0
===============================================

As you see the console messages 2 tests are passed and one is failed (test1 is failed, which crossed the suite time out 5000 milliseconds).

Timeout at method level takes precedence over suite level timeout
If you set time out at method level, then the method level timeout takes precedence over suite level time out.
Example
@Test(timeOut = 7000)
public void test1() {
 System.out.println("Going to sleep for 6 seconds");

 try {
  TimeUnit.SECONDS.sleep(6);
 } catch (InterruptedException e) {
  System.out.println("In catch block : " + e.getMessage());
 }

}

SuiteTimeOutTest.java
package com.sample.app.tests;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

public class SuiteTimeOutTest {

 @Test(timeOut = 7000)
 public void test1() {
  System.out.println("Going to sleep for 6 seconds");

  try {
   TimeUnit.SECONDS.sleep(6);
  } catch (InterruptedException e) {
   System.out.println("In catch block : " + e.getMessage());
  }

 }

 @Test
 public void test2() {
  System.out.println("Going to sleep for 4 seconds");

  try {
   TimeUnit.SECONDS.sleep(4);
  } catch (InterruptedException e) {
   System.out.println("In catch block : " + e.getMessage());
  }

 }

 @Test
 public void tes3() {
  System.out.println("Going to sleep for 2 seconds");

  try {
   TimeUnit.SECONDS.sleep(2);
  } catch (InterruptedException e) {
   System.out.println("In catch block : " + e.getMessage());
  }

 }

}

Re run ‘suiteTimeLimitDemo.xml’, you will get below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
Going to sleep for 2 seconds
Going to sleep for 6 seconds
Going to sleep for 4 seconds

===============================================
sanity test suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
=============================================== 

From the console messages, you can confirm that all the test cases are passed.


Previous                                                    Next                                                    Home

No comments:

Post a Comment