TestNG provides multiple ways to execute tests in separate threads. In testng.xml, if we set 'parallel' attribute on the
If we want to run methods/classes in separate threads, we need to set 'parallel' attribute on the
This helps us to run test methods / classes / tests in parallel. By using parallel execution, we can reduce the 'execution time' as tests are executed simultaneously in different threads.
In testNG we can achieve parallel execution by two ways. One with testng.xml file and we can configure an independent test method to run in multiple threads.
First let us look at basic example for Parallel Execution of Test Methods using 'parallel' attribute on the
We will create a class with Two test methods and try to execute in different threads.
package com.parallel;
import org.testng.annotations.Test;
public class TestParallelOne {
@Test
public void testCaseOne() {
//Printing Id of the thread on using which test method got executed
System.out.println("Test Case One with Thread Id:- "
+ Thread.currentThread().getId());
}
@Test
public void testCaseTwo() {
////Printing Id of the thread on using which test method got executed
System.out.println("Test Case two with Thread Id:- "
+ Thread.currentThread().getId());
}
}
The below is the simple testng.xml file, if you observe, we are defining two attributes 'parallel' and 'thread-count' at suite level. As we want test methods to be executed in parallel, we have provided 'methods'. And 'thread-count' attribute is to used to pass the number of maximum threads to be created.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
<test name="Regression 1">
<classes>
<class name="com.parallel.TestParallelOne"/>
</classes>
</test>
</suite>
Now run the above example and see the output, it should look like below :
The above result shows that two methods executed using different threads. When we run the same testng.xml again, the result may vary. The assigning of the thread is take care by the processor. So we can't say which thread is going to execute which method.
If say example, now there is an other test method which is added in the class.
Now if we execute the same testng.xml, we will get the output as below:
Here Two threads are created. Which ever thread completes the execution of one method, will pick and execute the other test method.
Comments
TestNG
Very good
GOOD
GOOD GOOD
So much details in your
So much details in your tutorial that too technical Loving it.. Thanks for the great things shared
Parallel Execution.
Hi Author,
I tried with the above e.g. however I am getting the expected output as you have displayed.
Is it required the hub and node to be up and running?
Or, just adding the parallel="methods" parameter along with the thread-count parameter will help in achieving the result.
Kindly revert.
Thanks
Prashant
Here two Methods are Running in Simultaneously or Concurrently
The Parallel="methods" will execute concurrently or simultaneously
Thanks its realy helpfull..
Thanks its realy helpfull..
suppose i have to process
suppose i have to process group and methods parallel is it possible to do it simultaneously.
Yes, We can do.
Yes, We can do.
Add new comment