Parallel Execution of test methods in TestNG

TestNG provides multiple ways to execute tests in separate threads. In testng.xml, if we set 'parallel' attribute on the tag to 'tests', testNG will run all the ‘@Test’ methods in tag in the same thread, but each tag will be in a separate thread.

If we want to run methods/classes in separate threads, we need to set 'parallel' attribute on the tag to 'methods' / 'classes'

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 tag with 'method'..

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 {

	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());

	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 "">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Regression 1">
      <class name="com.parallel.TestParallelOne"/>

Now run the above example and see the output, it should look like below :

test methods in parallel

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:

test methods execution parallel

Here Two threads are created. Which ever thread completes the execution of one method, will pick and execute the other test method.

Test Frameworks: 


Very good


So much details in your tutorial that too technical Loving it.. Thanks for the great things shared

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.


Add new comment