@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 whenever you want to run a test method multiple times and in parallel, you will need this attribute. The method will be invoked from multiple threads as specified by the invocationCount attribute.

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.

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 a lesser amount of 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 feel free to write us in the comments or mail us at [email protected]

Other @Test attributes
Liked the article? Share this on

Leave a Comment

Your email address will not be published.