How to skip test if its taking time more than certain amount of time in TestNG?

Viewed 17

I am having API test suite where few tests taking a great amount time causing test suite to run more than 5-6+ hrs. Is there any way to skip certain tests which exceeds run time for that particular test in TestNG.

1 Answers

First you implement IInvokeMethodListener where you do:

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult){
  if(testResult.getThrowable() instanceof ThreadTimeoutException){
    testResult.setStatus(ITestResult.SKIP);
  }
}

Say you implemented it in MyImpl class. Then you could have:

@Listeners({MyImpl.class})
public class MyTest {

  @Test(timeOut = 1)
  public void test(){
    // Do long task that takes more than 1 ms
    // The test will be skipped
  }

}
Related