Some test cases may expect longer time to excute, in order to avoid take more than the specified number of milliseconds or not to block TestNG forever, we specify a timeout in the @Test annotation, if a unit case takes more than the expected time, ThreadPoolExecutor will be interrupted and test case will be terminated and mark it as failure.

package com.asjava;

import org.testng.annotations.*;

public class TestNGTimeTest {

    @Test(timeOut = 1000)
    public void waitLongTime() throws Exception {
        Thread.sleep(1001);
    }
}

TestNG provides parameter invocationCount in annotation test to keep watch on runing asynchronous tests in multiple threads.  timeOut and invocationCount always be used together, because while testing in multiple threads, use of timeOut can ask every asynchronous call to return. the value of successPercentage is expected the overall test passes even if only a percent of the invocation passes:

package com.asjava;

import org.testng.annotations.*;

public class TestNGTimeTest {

    @Test(timeOut = 1000, invocationCount = 100, successPercentage = 98)
    public void testInvocationCount() throws Exception {
        Thread.sleep(100);
        System.out.println("waitForAnswer");
    }
}
annotations