What is the use of "threadPoolSize" and "SingleThreaded" attribute in @Test Annotation?

What is the use of "threadPoolSize" and "SingleThreaded" attribute in @Test Annotation?

ThreadPoolSize Attribute

The threadPoolSize method will be invoked from multiple threads as specified by the invocationCount.

Note: this attribute is ignored if invocationCount is not specified

In the below program, we are expecting 2 Thread to invoke the method.

Java Code-

public class Testing_ThreadPool {
	
@Test(invocationCount = 4, threadPoolSize = 2)
public void testNG1()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName+":"+Thread.currentThread().getId());
}
}

Outcome-

 
[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1:15
Method name is :testNG1:16
Method name is :testNG1:15
Method name is :testNG1:16
PASSED: testNG1
PASSED: testNG1
PASSED: testNG1
PASSED: testNG1

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


===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
Related topic-
















SingleThreaded Annotation-

This attribute can accept only the boolean value, So value can eighter be true or false. If this value is set as true then all the methods on in this test class will run in the same thread, even if the tests are currently being run with parallel="methods" and thread count is greater then 1.

This attribute can only be used at the class level and it will be ignored if used at the method level.

Java Code-

    
    @Test(singleThreaded=true)
public class Testing_singleThreaded {
	
@Test()
public void testNG1()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName+": Thread name is :- "+Thread.currentThread().getId());
}
@Test()
public void testNG2()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName+": Thread name is :- "+Thread.currentThread().getId());
}

} 

XML-

<?xml version="1.0" encoding="UTF-8"?>
<suite name="group" parallel="methods" thread-count="2">
<test name= "name1">
<classes>
<!--  <class name="org.Testngpro.Testing_Grouping"></class>-->
<class name="org.Testngpro.Testing_singleThreaded"></class>
</classes>
</test>
</suite>

Outcome-

 
[RemoteTestNG] detected TestNG version 7.4.0
[TestNGContentHandler] [WARN] It is strongly recommended to add "" at the top of the suite file [F:\eclipse\Practice_2022\TestNGMethod\singleThread.xml] otherwise TestNG may fail or not work as expected.
Method name is :testNG1: Thread name is :- 17
Method name is :testNG2: Thread name is :- 17

===============================================
group
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

No comments:

Post a Comment