TestNG Parallel execution
TestNG Parallel execution
sequential: one after the other ....going in sequence one after other
parallel: side by side ...doing multiple tasks side by side
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.
By Default TestNG creates "one thread for Execution" and Executes all test in
sequential order.
In following scenario, parallel execution will not happens i.e Sequentail value
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)
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
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
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
@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