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

Parallel Execution of Classes in TestNG

TestNG provides an ability to run test classes in parallel. By using parallel execution of classes, each class will be started and executed simultaneously in different threads.

Let us look at basic example for Parallel Execution of Classes using testng.xml.

We will create a Two classes with Two test methods each and try to execute in different threads.

Create class and name it as : TestParallelClassOne.java

package com.parallel;

import org.testng.annotations.Test;

public class TestParallelClassOne {

	@Test
	public void testCaseOne() {
		// Printing class name and Id of the thread on using which test method got executed
		System.out.println("Test Case One in " + getClass().getSimpleName()
				+ " with Thread Id:- " + Thread.currentThread().getId());
	}

	@Test
	public void testCaseTwo() {
		 //Printing class name and Id of the thread on using which test method got executed
		System.out.println("Test Case two in " + getClass().getSimpleName()
				+ " with Thread Id:- " + Thread.currentThread().getId());
	}

}

Create class and name it as : TestParallelClassTwo.java

package com.parallel;

import org.testng.annotations.Test;

public class TestParallelClassTwo {

	@Test
	public void testCaseOne() {
		//Printing class name and Id of the thread on using which test method got executed
		System.out.println("Test Case One in " + getClass().getSimpleName()
				+ " with Thread Id:- " + Thread.currentThread().getId());
	}

	@Test
	public void testCaseTwo() {
		//Printing class name and Id of the thread on using which test method got executed
		System.out.println("Test Case Two in " + getClass().getSimpleName()
				+ " with Thread Id:- " + Thread.currentThread().getId());
	}

}

The below is the testng.xml file

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="classes" thread-count="2">
  <test name="Test 1">
    <classes>
      <class name="com.parallel.TestParallelClassOne"/>
      <class name="com.parallel.TestParallelClassTwo"/>
    </classes>
  </test>
</suite>

In the above testng.xml file, we have defined two attributes 'parallel' and 'thread-count' at suite level. As we want classes to be executed in parallel, we have provided 'parallel="classes''. And 'thread-count' attribute is to used to pass the number of maximum threads to be created.

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

The above result shows that two classes executed using different threads. When we run the same testng.xml again, the result may vary. As The thread scheduler dispatches the various threads on the available processors, and each thread gets some processor time, each in his turn. But the processor, the order and the time assigned to each thread is up to the OS thread scheduler, and we does not guarantee the order of execution.

Test Frameworks: 

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.