Wednesday 11 March 2020

TestNG: Specify timeout for the testcase

Sometimes we may want to finish the test case in given amount of time. For example,
a.   To test maximum time taken by a sorting algorithm.
b.   Testing response time from REST endpoint etc.,

How to specify timeout for a test case?
Using timeOut attribute we can set time out for a test case. For example, below test case fail, if the test do not complete in 5 seconds (5000 milliseconds).

@Test(timeOut = 5000)
public void test1() {
         ....
         ....
}
  
TimeoutTest.java
package com.sample.app.tests;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

public class TimeoutTest {

 @Test(timeOut = 5000)
 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());
  }

 }

}



Previous                                                    Next                                                    Home

No comments:

Post a Comment