Skip to content Skip to navigation
Check Our New Demo Website!

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 {

	@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 :

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: 

Comments

Very good

GOOD GOOD

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.