TestNG - Run the same test method multiple times

By xngo on June 28, 2019

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

TestNG - invocationCount results

Github

  • https://github.com/xuanngo2001/java-testng/blob/master/src/net/openwritings/testng/InvocationCountTest.java

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.