Parallel Test Execution in Selenium using TestNG

What is Parallel Testing in TestNG?

Parallel Testing is a testing technique in which multiple tests are executed simultaneously in different threads to reduce execution time.

It allows us to execute multiple tests at the same time across different environments instead of running tests one by one or sequentially. Hence, it is also called parallel test execution in Selenium.

Parallel testing helps us to run test methods/classes/tests in parallel. By using parallel test execution, we can reduce the ‘execution time’ as tests will be executed simultaneously in different threads.

Parallel test execution in Selenium

How to run Parallel Tests, Classes, & Methods in Selenium using TestNG XML File?


In Selenium, we can run our test methods/classes/tests in parallel by using the “parallel” attribute for Test Suite in the testng.xml file.

The parallel attribute for on the <suite> tag can accept one of the following values:

1. <suite name = "Parallel Test Suite" parallel = "methods" thread-count = "3">

2. <suite name = "Parallel Test Suite" parallel = "tests" thread-count = "3">

3. <suite name = "Parallel Test Suite" parallel = "classes" thread-count = "3">

4. <suite name = "Parallel Test Suite" parallel = "instances" thread-count = "3">

1. parallel = “methods“: TestNG will execute all the methods annotated with @Test annotation in separate threads (i.e. parallel). Dependent methods will also run in separate threads in the order that we specified.

2. parallel = “Tests“: TestNG will run all the test cases/test methods in the same <test> tag in the same thread but each <test> tag will be run in different threads.

3. parallel = “classes“: TestNG will run all the test cases (test methods) in the same class in the same thread but each Java class will run in a separate thread.

4. parallel = “instances“: TestNG will run all test cases in the same instance in the same thread, but two methods on two different instances will be run in different threads.

The attribute “thread-count” specifies how many threads should be allocated for this execution.


Let’s start with a basic example for Parallel Execution of Test Methods using “parallel” attribute for the <suite> tag in the testng.xml configuration file.

Parallel Execution of Test Methods in TestNG


In this example, we will create a class with two test methods and try to execute them in different threads. So, let’s start coding.

Program source code 1:

package parallelTesting; 
import org.testng.annotations.Test; 
public class parallelExecutionTestMethod 
{ 
@Test 
public void firstTest() 
{ 
  System.out.println("First Test Method"); 
} 
@Test 
public void secondTest() 
{ 
   System.out.println("Second Test Method"); 
  } 
}

Now create a testng.xml file and configure it. In this configuration file, we will define two attributes “parallel” and “thread-count” at suite level for execution of test methods in parallel.

Here, the attribute “thread-count” is to be used to pass the number of maximum threads to be created.

<suite name = "Parallel Test Suite" parallel = "methods" thread-count = "2"> 
<test name = "Test Methods"> 
<classes> 
         <class name = "parallelTesting.parallelExecutionTestMethod"/> 
</classes> 
</test> 
</suite>

Run the above configure testng.xml file as TestNG Suite and see the output. It should look like below in the screenshot.

Result of parallel execution test methods

As you can observe in the above result that two test methods have been executed in parallel in separate threads. Both test methods have taken time 0.017s for complete execution. The result may vary when you will run the same testng.xml again.

Parallel Execution of Classes in TestNG


In this section, we will run test classes in parallel. During parallel execution of test classes, each java class will be started and executed simultaneously in separate threads.


Let’s take a basic example for parallel execution of classes using testng.xml file. In this example, we will create two test classes having two test methods each and try to execute them in different threads.

Now, create a java class by the name “ClassOne” and define two test methods. Look at the following source code.

Program source code 2:

package parallelTesting; 
import org.testng.annotations.Test; 
public class ClassOne 
{ 
@Test 
public void firstTest() 
{ 
  System.out.println("First test method in ClassOne"+" " +"Thread Id: " +Thread.currentThread().getId()); 
} 
@Test 
public void secondTest() 
{ 
   System.out.println("Second test method in ClassOne"+ " " +"Thread Id: " +Thread.currentThread().getId()); 
 } 
} 
package parallelTesting; 
import org.testng.annotations.Test; 
public class ClassTwo 
{ 
@Test 
public void firstTest() 
{ 
   System.out.println("First test method in ClassTwo"+ " " +"Thread Id: " +Thread.currentThread().getId()); 
} 
@Test 
public void secondTest() 
{ 
   System.out.println("Second test method in ClassTwo"+ " " +"Thread Id: " +Thread.currentThread().getId()); 
 } 
}

Now create a new testng.xml file by name”classes-testng.xml” and copy the following code into it.

<suite thread-count = "2" name = "Parallel Test Suite" parallel = "classes"> 
<test name = "Test Classes"> 
<classes> 
     <class name = "parallelTesting.ClassOne"/> 
     <class name = "parallelTesting.ClassTwo"/> 
</classes> 
</test> 
</suite>

Run testng.xml file as TestNG Suite and observe the following output on your console.

Output: 
       First test method in ClassOne Thread Id: 9 
       First test method in ClassTwo Thread Id: 10 
       Second test method in ClassOne Thread Id: 9 
       Second test method in ClassTwo Thread Id: 10 
      =============================================== 
      Parallel Test Suite Total tests run: 4, Failures: 0, Skips: 0 
      ===============================================

Now observe the above output, TestNG has run all the test cases (test methods) in the same class in the same thread but each Java class has run in different threads.

Results of running suite:
Parallel execution of classes using testng xml file

Now observe running suite as shown in above screenshot, ClassOne, and ClassTwo both have run at the same time in parallel due to which firstTest() methods of both classes and secondTest() methods of both classes have the same execution time.

Parallel Execution of Tests in TestNG


In this section, we will consider program source code 2 for parallel execution of tests and configure testng.xml file according to the following code.

First, create a new testng.xml file by name “tests-testng.xml” and then configure it.

<suite thread-count = "2" name = "Parallel Test Suite" parallel = "tests"> 
<test name = "ClassOne"> 
<classes> 
     <class name = "parallelTesting.ClassOne"/> 
</classes> 
</test> 
<test name = "ClassTwo"> 
<classes> 
      <class name = "parallelTesting.ClassTwo"/> 
</classes> 
</test> 
</suite>

Run the testng.xml file as TestNG Suite and observe the following output on the console.

Output: 
       First test method in ClassOne Thread Id: 9 
       First test method in ClassTwo Thread Id: 10 
       Second test method in ClassOne Thread Id: 9 
       Second test method in ClassTwo Thread Id: 10
      =============================================== 
      Parallel Test Suite Total tests run: 4, Failures: 0, Skips: 0 
      ===============================================

As you can see in the output, TestNG has run all the test methods in the same <test> tag in the same thread but each <test> tag has run in different threads.

Parallel Execution of Tests in TestNG

As you can see in the screenshot, both tests ClassOne and ClassTwo have run in parallel at the same time.


Hope that this tutorial has covered almost all important points related to parallel test execution in Selenium using TestNG with useful example programs. I hope that you will have understood parallel execution of test methods/classes/tests in TestNG and enjoyed related coding.
Thanks for reading!!!
Next ⇒ TestNG Assert⇐ PrevNext ⇒

Please share your love