@Test annotation attributes – threadPoolSize attribute

This post will discuss the threadPoolSize attribute of @Test annotation in TestNG. So, let’s dive in.

Now, what is the use of threadPoolSize attribute? The answer is that you will need this attribute whenever you want to run a test method multiple times and in parallel. The method will be invoked from multiple threads as specified by the invocationCount attribute.

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class CodekruTest {
 
    @Test(invocationCount = 5, threadPoolSize = 5)
    public void testMethod() {
        int expected = 5;
        int actual = 5;
        Assert.assertEquals(actual, expected);
    }
 
}

Output –

Executing the test
Executing the test
Executing the test
Executing the test
Executing the test
PASSED: testMethod
PASSED: testMethod
PASSED: testMethod
PASSED: testMethod
PASSED: testMethod

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0

You may want to ask what invocationCount is doing there. We won’t be able to use threadPoolSize without using the invocationCount attribute.

Note: threadPoolSize attribute will be ignored if invocationCount is not defined.

Let’s see this with an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class CodekruTest {
 
    @Test(threadPoolSize = 5)
    public void testMethod() {
        int expected = 5;
        int actual = 5;
        System.out.println("Executing testMethod");
        Assert.assertEquals(actual, expected);
    }
 
}

Output –

Executing testMethod
PASSED: testMethod

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

We can see that the test case was executed only once.

When can we use the threadPoolSize attribute? There may be a case where we want to run a test method multiple times( say 100 ). Then it can take a substantial amount of time to run 100 test cases one by one. So, to decrease the execution time, we can use threadPoolSize there with a value ( say 5 ), and now the test case will run in parallel instances, and the cases will be executed in less time.

Please refer to this post to learn about invocationCount.

We hope that you find this helpful. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Other @Test attributes
Liked the article? Share this on

2 thoughts on “@Test annotation attributes – threadPoolSize attribute”

  1. I am using Threadpoolsize attribute and invocation count attribute , tests are running parallel on the same browser instances instead of different instances of browser in parallel.

    1. You are making a driver instance in the @BeforeClass annotated method, which will only run once per class execution. Can you try to do it in a @BeforeMethod annotated method and let us know if it works?

Leave a Comment

Your email address will not be published. Required fields are marked *