03
Sep

Parallel execution strategies to understand Thread creation logic for test execution

Parallel execution strategies to understand Thread creation logic for test execution



TestNG provides the following parallel execution strategies:
============================================================

	parallel="tests"	=>  specifies all methods in all classes should be executed in same thread
	parallel="classes"	=>  specifies all methods in a class should be executed in same thread
	parallel="methods"	=>  specifies all methods in all classes should be executed in unique thread


for example:

To execute all methods (in all classes in TestNG Package) in unique threads:
=============================================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
	<test thread-count="5" name="instances">
		<packages>
			<package name="testNGSerialClassParallelMethodsExecution.*" />
		</packages>
	</test>
</suite> 



Output:
RunninggetClass().getName().anotherTestMethod() on Thread [13]
RunninggetClass().getName().testMethod() on Thread [14]
RunninggetClass().getName().anotherTestMethod() on Thread [15]
RunninggetClass().getName().testMethod() on Thread [16]


To execute all methods (in a given classes in TestNG Package) in unique threads:
=================================================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" group-by-instances="true"
	object-factory="testNGSerialClassParallelMethodsExecution.MyObjectFactory">
	<test thread-count="5" name="instances">
		<packages>
			<package name="testNGSerialClassParallelMethodsExecution.*" />
		</packages>
		<classes>
			<class name="testNGSerialClassParallelMethodsExecution.ClassOne" />
			<class name="testNGSerialClassParallelMethodsExecution.ClassTwo" />
		</classes>
	</test>
</suite> 


Output:
RunninggetClass().getName().anotherTestMethod() on Thread [13]
RunninggetClass().getName().testMethod() on Thread [13]
RunninggetClass().getName().anotherTestMethod() on Thread [14]
RunninggetClass().getName().testMethod() on Thread [14]


To execute all methods (in any classes of TestNG Package) in same threads:
============================================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" group-by-instances="true"
	object-factory="testNGSerialClassParallelMethodsExecution.MyObjectFactory">
	<test thread-count="5" name="instances">
		<packages>
			<package name="testNGSerialClassParallelMethodsExecution.*" />
		</packages>
		<classes>
			<class name="testNGSerialClassParallelMethodsExecution.ClassOne" />
			<class name="testNGSerialClassParallelMethodsExecution.ClassTwo" />
		</classes>
	</test>
</suite> 

Output:
RunninggetClass().getName().anotherTestMethod() on Thread [13]
RunninggetClass().getName().testMethod() on Thread [13]
RunninggetClass().getName().anotherTestMethod() on Thread [13]
RunninggetClass().getName().testMethod() on Thread [13]