TestNG Parallel Execution

Filed Under: TestNG

We can run TestNG tests in parallel. This feature is only available when we are using TestNG XML suite for executing our test cases.

TestNG Default Tests Execution

When we run TestNG class, methods are executed in order of the alphabetical appearance of their names. Let’s look at a simple TestNG test class.


package com.journaldev.parallel;

import java.time.LocalDateTime;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class NewTest {
	@Test(dataProvider = "dp")
	public void foo(Integer n) {
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Foo Executing Input = " + n);
	}
	
	@Test(dataProvider = "dp")
	public void bar(Integer n) {
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Bar Executing Input = " + n);
	}

	@DataProvider
	public Object[] dp() {
		Object[] ints = new Object[100];
		for (int i = 0; i < 100; i++)
			ints[i] = i;
		return ints;
	}

	@AfterTest
	public void afterTest() {
		System.out.println("After Test Time = " + LocalDateTime.now());
	}

	@BeforeTest
	public void beforeTest() {
		System.out.println("Before Test Time = " + LocalDateTime.now());
	}

}

When we run above class, it will generate output like below.


Before Test Time = 2018-06-01T15:03:21.406219
Bar Executing Input = 0
Bar Executing Input = 1
...
Bar Executing Input = 99
Foo Executing Input = 0
Foo Executing Input = 1
...
Foo Executing Input = 99
After Test Time = 2018-06-01T15:03:24.058081

Notice that bar() method is getting executed first, once all test executions are over then foo() methods are executed.

We will get the similar result with below XML suite file.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
	<test name="TestNGXMLTest Test" verbose="2">
		<classes>
			<class name="com.journaldev.parallel.NewTest" />
		</classes>
	</test>
</suite>

TestNG Methods in Parallel

Since both the test methods are unrelated, we can run them in parallel and save time. We will have to add parallel and thread-count to our test to achieve this.

Below is our updated XML suite file.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
	<test name="TestNGXMLTest Test" verbose="2" parallel="methods" thread-count="2">
		<classes>
			<class name="com.journaldev.parallel.NewTest" />
		</classes>
	</test>
</suite>

When we run the test suite, we will get following output.


Before Test Time = 2018-06-01T15:06:38.268674
Bar Executing Input = 0
Foo Executing Input = 0
...
Foo Executing Input = 99
After Test Time = 2018-06-01T15:06:39.572976

Notice that we almost cut the test execution time to half by running test methods in parallel.

TestNG Parallel Execution Important Points

  1. We can use parallel attribute for both test and suite elements.
  2. Possible values for parallel attribute are: methods, tests, classes and instances. We have already seen how to execute methods in parallel.
  3. In older TestNG versions, true and false were also allowed for parallel values, they have been deprecated now.

TestNG Running Classes in Parallel

Let's say we want to run our test classes in parallel. We also want our methods inside the test classes to be running in parallel. We can do this by using parallel="classes" at suite level and parallel="methods" at test level.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite" parallel="classes">
	<test name="TestNGXMLTest Test" verbose="2" parallel="methods"
		thread-count="10">
		<classes>
			<class name="com.journaldev.parallel.NewTest" />
			<class name="com.journaldev.utils.TestUtils" />
			<class name="com.journaldev.main.Test5" />
		</classes>
	</test>
</suite>

TestNG Suite Running Parallel Tests

Let's look at a more complex example, where we are running TestNG suite tests in parallel. Again some of these tests are configured to execute classes/methods in parallel.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite" parallel="classes">
	<test name="TestNGXMLTest Test 1" verbose="2" parallel="tests"
		thread-count="10">
		<classes>
			<class name="com.journaldev.main.Test5" />
		</classes>
	</test>
	<test name="TestNGXMLTest Test 2" verbose="2" parallel="classes"
		thread-count="10">
		<classes>
			<class name="com.journaldev.utils.TestUtils" />
			<class name="com.journaldev.main.Test5" />
		</classes>
	</test>
	<test name="TestNGXMLTest Test 3" verbose="2" parallel="methods"
		thread-count="10">
		<classes>
			<class name="com.journaldev.parallel.NewTest" />
		</classes>
	</test>
</suite>

That's all for running TestNG test suites in parallel, in different modes.

You can download the example code from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages