0% found this document useful (0 votes)
3 views

TestNG Parallel execution

The document explains how to execute tests in parallel using TestNG, highlighting the benefits of parallel execution for saving time compared to sequential execution. It details the configuration needed in the TestNG XML file, including the 'parallel' and 'thread-count' attributes, and provides examples of how to set these for different levels of parallelism. Additionally, it covers the use of invocation count, invocation timeout, and thread pool size in TestNG to manage test execution more effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

TestNG Parallel execution

The document explains how to execute tests in parallel using TestNG, highlighting the benefits of parallel execution for saving time compared to sequential execution. It details the configuration needed in the TestNG XML file, including the 'parallel' and 'thread-count' attributes, and provides examples of how to set these for different levels of parallelism. Additionally, it covers the use of invocation count, invocation timeout, and thread pool size in TestNG to manage test execution more effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Execute your tests in parallel using TestNG

sequential: one after the other ....going in sequence one after other
parallel: side by side ...doing multiple tasks side by side

sequential execution: executing test "without mentioning parallel"


parallel execution means executing test side by side ( not one after the
other) ...it save lot of time

Let say following is execution time for executing individual test


Testcase TimetakenToExecute
1 5
2 10
3 5
4 5
5 10

To run all above test in squential manner it will take around 35 seconds
but if we same test in parallel( side by side) at max it will take 11 seconds
In realtime scenarios we might have 1000 of tests and we may want to run it in
parallel to save time.
But we cant run all 1000 cases parallel because of resource limitation on our
computer ( CPU/Memory/Network bandwidth)
if we want to run "n" testcases/threads in parallel , our system also should
support that
Every system can support atleast 2 or 3 threads i.e we can run 2 or 3 testcases in
parallel.

1. what is parallel execution


running tests side by side
2. why do we need parallel execution
to save execution time
3. How to Perform parallel execution

By Default TestNG creates "one thread for Execution" and Executes all test in
sequential order.

To run the test in parallel we have to do this through Testng.xml


if we see the structure of testNg file we have
<suite>
<test>
<classes>
<class>
<methods>
</methods>
</class>
</classes>
</test>
</suite>

for parallel execution we need two attribute :


1. parallel which can take none, true, false, tests, classes, methods, instances
instances is used only in case of factory annotation
2. thread-count which take +ve integer value ( creating -ve no. of thread doesnt
make sense and will raise IllegalArgumentException)
default value for thread-count is 5 .... if parallel has valid value 5 is
considered if not explicitely mentioned
we can decide parallel execution either at suite or test level by specifying
parallel attribute with appropriate value and thread-count

In following scenario, parallel execution will not happens i.e Sequentail value

<suite parallel="none" thread-count=2> <==thread count is ignored as


parallel="none"
<suite parallel="false" thread-count=2> <==thread count is ignored as
parallel="false"
<suite parallel=true thread-count=1> <== as thread count=1

For parallel execution , "parallel" should have either of the following values
true, tests, classes, methods, instances
and "thread=count" should be more than one

e.g.
1)

<suite name="Suite" parallel="tests" thread-count="2">


<test name="Test1">
<classes >
<class name="com.hyr.parallelTest.TestClass1"/>
</classes>
</test> <!-- Test -->
<test name="Test2">
<classes>
<class name="com.hyr.parallelTest.TestClass2"/>
</classes>
</test>
<test name="Test3">
<classes>
<class name="com.hyr.parallelTest.TestClass3"/>
</classes>
</test>
<test name="Test4">
<classes>
<class name="com.hyr.parallelTest.TestClass4"/>
</classes>
</test>
</suite>

we are asking testNg to create 2 threads of execution and run test in parallel
Among all the test present in suite .....at max 2 test will run in parallel( side
by side ) and once one of them finish ,other test will trigger
so at any point in time only 2 tests are running in parallel
But within test thread all classes and methods are executed in sequence...
as <test> tag are inside <suite> tag .... parellel=tests make sense only in
<suite> tag

TestClass1 >> testMethod1 TestNG-tests-1 21


TestClass1 >> testMethod2 TestNG-tests-1 21
TestClass1 >> testMethod3 TestNG-tests-1 21
TestClass1 >> testMethod4 TestNG-tests-1 21
TestClass4 >> testMethod13 TestNG-tests-1 21
TestClass4 >> testMethod14 TestNG-tests-1 21
TestClass4 >> testMethod15 TestNG-tests-1 21
TestClass4 >> testMethod16 TestNG-tests-1 21
TestClass2 >> testMethod5 TestNG-tests-2 22
TestClass2 >> testMethod6 TestNG-tests-2 22
TestClass2 >> testMethod7 TestNG-tests-2 22
TestClass2 >> testMethod8 TestNG-tests-2 22
TestClass3 >> testMethod10 TestNG-tests-2 22
TestClass3 >> testMethod11 TestNG-tests-2 22
TestClass3 >> testMethod12 TestNG-tests-2 22
TestClass3 >> testMethod9 TestNG-tests-2 22

if parallel="tests" thread-count=3 ...so 3 threads of executions are started and


3 tests are invoked in parallel (side by side)
when one test finished in test (here test containing TestClass2),other test is
ivoked

TestClass1 >> testMethod1 TestNG-tests-1 20


TestClass1 >> testMethod2 TestNG-tests-1 20
TestClass1 >> testMethod3 TestNG-tests-1 20
TestClass1 >> testMethod4 TestNG-tests-1 20

TestClass2 >> testMethod5 TestNG-tests-2 21


TestClass2 >> testMethod6 TestNG-tests-2 21
TestClass2 >> testMethod7 TestNG-tests-2 21
TestClass2 >> testMethod8 TestNG-tests-2 21

TestClass3 >> testMethod10 TestNG-tests-3 22


TestClass3 >> testMethod11 TestNG-tests-3 22
TestClass3 >> testMethod12 TestNG-tests-3 22
TestClass3 >> testMethod9 TestNG-tests-3 22

TestClass4 >> testMethod13 TestNG-tests-2 21


TestClass4 >> testMethod14 TestNG-tests-2 21
TestClass4 >> testMethod15 TestNG-tests-2 21
TestClass4 >> testMethod16 TestNG-tests-2 21

2)
<suite name="Suite" parallel="classes" thread-count="2">
<test name="Test1">
<classes >
<class name="com.hyr.parallelTest.TestClass1"/>
</classes>
</test> <!-- Test -->
<test name="Test2">
<classes>
<class name="com.hyr.parallelTest.TestClass2"/>
</classes>
</test>
<test name="Test3">
<classes>
<class name="com.hyr.parallelTest.TestClass3"/>
</classes>
</test>
<test name="Test4">
<classes>
<class name="com.hyr.parallelTest.TestClass4"/>
</classes>
</test>
</suite>
we are asking testNg to create 2 threads of execution and run classes in parallel.
Remember here test will be picked sequentally but If test have multiple classes,
then they will be run in parallel.
if test has only one class then it will run ( which appears same as sequentail)
Among all the classes present in test .....at max 2 classes will run in
parallel( side by side ) and once one of them finish ,other class will trigger
so at any point in time only 2 classes are running in parallel
But within class thread all methods are executed in sequence...
as <classes> tag are inside <test> tag .... parellel=classes make more sense
only in <test> tag for better control of execution

similar things is true for parallel="methods"


we are asking testNg to create 2 threads of execution and run methods in parallel.
Remember here test and classes will be picked sequentally but If class have
multiple methods, then they will be run in parallel.
as <methods> tag are inside <classes> tag .... parellel=methods make more sense
only in <test> tag for better control of execution

but if provide parallel=methods or parallel=classes in <suite> tag then given no


of threads are created for each test within suite
tests are picked sequentailly for execution ....and based on parallel arguments
value "methods" or "classes" will be run in parallel
if thread-count=2 and we have 4 tests ....for parallel=methods or parallel=classes
total 8 threads are created 2 threads/test to execute method or class
===================================================================================
=========
P24> How to use Invocation Count, Invocation timeout TrheadPool size in TestNG?
These are the annotation available in @Test annotations

Lets say we have test method with code for some functionality and we want to run
same code 5 time
what we can do is use for loop inside test method to repeat code for 5 time
but what if in any one iteration test failed (may be due to synchronization issue
in selenium)? remaining iteration wont be run ...right?
and we have to things to handle it

Instead of doing these , TestNg provide very good feature call invocation count
which we can use to run
the test methods for given no. of time
1) What is invocation count
is attribute in @Test annoation which tells TestNg how many time Test method
should be executed
2) why do we need invocation count
instead of using loop (while, for) which may fail due to synchronization issue
we can use invocationCount to repeat the test
3) How to use invocation count
@Test(invocationCount=5) <====it uses integer value

1) What is invocation timeout


2) why do we need invocation timeout
3) How to use invocation timeout

similar to timeout concept but application to invocation count

@Test(invocationCount=5,invocationTimeOut=10000) <==timeout value is in


"miliseconds"
this will check if 5 invocation have completed in 10 sec else it will fail test
1) What is Threadpool size
2) why do we need Threadpool size
3) How to use Threadpool size

similar to thread-count concept but applicable to invocation count

@Test(invocationCount=4,threadPoolSize=2)
this will invoke test 4 times but in 2 threads
so at any point in time, 2 invocation are happening

You might also like