Invocation Count in TestNG

  

What is invocationCount in TestNG?

In TestNG, the invocationCount attribute is used to execute a test method multiple times.


Explanation:

  • invocationCount is an attribute of the @Test annotation.

  • When set, it tells TestNG to run the same test method multiple times.

  • This is useful for:

    • Repeating tests for reliability (e.g., flaky tests).

    • Load or stress testing a piece of code.


Syntax:

@Test(invocationCount = n)

Where n is the number of times the method should run.



Java Code example:

import org.testng.annotations.Test;

public class InvocationCountExample {

    @Test(invocationCount = 5)
    public void repeatedTest() {
        System.out.println("Running test method - Thread ID: " + Thread.currentThread().getId());
    }
}


Output:

Running test method - Thread ID: 1
Running test method - Thread ID: 1
Running test method - Thread ID: 1
Running test method - Thread ID: 1
Running test method - Thread ID: 1

You’ll see the method executed 5 times.

Advanced Usage with Parallel Execution (Optional):

If you want to execute these invocations in parallel, you can combine it with threadPoolSize.

@Test(invocationCount = 5, threadPoolSize = 3)
public void repeatedTestParallel() {
    System.out.println("Running test - Thread: " + Thread.currentThread().getId());
}

This will run 5 times using 3 threads in parallel.



Important Points:

AttributeDescription
invocationCountNumber of times to run the test method
threadPoolSizeNumber of threads to run invocations concurrently

No comments:

Post a Comment