InvocationCount in TestNG
invocationCount is an attribute of the @Test annotation that tells TestNG to run a test method N times before proceeding to the next test.
Basic example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class InvocationCountExample {
@Test(invocationCount = 3)
public void getTitle() {
System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.pavantestingtools.com/");
driver.manage().window().maximize();
System.out.println("Website Title: " + driver.getTitle());
driver.quit();
}
@Test
public void secondTest() {
System.out.println("This will be executed at the end");
}
}
Output: getTitle() runs 3 consecutive times, then secondTest() runs once.
invocationCount with threadPoolSize (parallel invocations):
@Test(invocationCount = 5, threadPoolSize = 3)
public void parallelTest() {
System.out.println("Running in thread: " + Thread.currentThread().getId());
}
This runs the test 5 times using 3 parallel threads simultaneously.
Use cases:
- ✓Stability testing — Run a flaky test multiple times to confirm it consistently passes
- ✓Load simulation — Repeat an action to simulate multiple user interactions
- ✓Stress testing — Verify a feature holds up under repeated execution
invocationCount vs @DataProvider:
| Feature | invocationCount | @DataProvider |
|---|---|---|
| Same data each run | Yes | No |
| Different data each run | No | Yes |
| External data source | No | Yes |
| Simple repetition | Yes | Overkill |
Key point: invocationCount runs the exact same test logic N times without changing any data — useful for flaky test analysis and stability checks.
