TestNG provides an annotation where you can define the number of times a method should repeatedly run. It is called invocationCount.
The code example below will execute printTime()
test method 3 times.
import org.testng.annotations.Test; public class InvocationCountTest { @Test(description="Run this method multiple times.", invocationCount=3) public void printTime() { System.out.println(System.currentTimeMillis()); try { Thread.sleep(1000); // Pause for 1 second. } catch(InterruptedException ex) { ex.printStackTrace(); } } }
Output
Github
- https://github.com/xuanngo2001/java-testng/blob/master/src/net/openwritings/testng/InvocationCountTest.java