</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

What is InvocationCount in TestNG?

invocationCount is a @Test attribute that defines how many times a test method should be executed consecutively before moving to the next test.

Answer

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:

Java
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):

Java
@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:

FeatureinvocationCount@DataProvider
Same data each runYesNo
Different data each runNoYes
External data sourceNoYes
Simple repetitionYesOverkill

Key point: invocationCount runs the exact same test logic N times without changing any data — useful for flaky test analysis and stability checks.

Follow AutomateQA

Related Topics