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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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 |
When we run above class, it will generate output like below.
1 2 3 4 5 6 7 8 9 10 11 12 |
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.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://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.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://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.
1 2 3 4 5 6 7 8 |
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
- We can use
parallel
attribute for both test and suite elements. - Possible values for parallel attribute are: methods, tests, classes and instances. We have already seen how to execute methods in parallel.
- In older TestNG versions,
true
andfalse
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://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.