Parallel Test Execution in Selenium using TestNG

Parallel Test Execution in Selenium using TestNG

Table of Contents

What is Parallel Testing in TestNG?

Parallel Testing is a technique where you want to run multiple tests simultaneously in different threads to reduce the execution time. It allows us to run multiple tests at the same time across multiple environments instead of running tests one by one or in sequential order. Hence, it is called a parallel test execution in selenium.

Parallel testing helps us to run classes, test methods, tests in parallel. We can reduce the execution time as tests will get executed simultaneously by using parallel test execution.

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

In Selenium, by using the ā€œparallelā€ attribute we can execute our classes, test methods, and tests in parallel 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 = “4”>
  2. <suite name = “Parallel Test Suite” parallel = “classes” thread-count = “4”>
  3. <suite name = “Parallel Test Suite” parallel = “tests” thread-count = “4”>
  4. <suite name = “Parallel Test Suite” parallel = “instances” thread-count = “4”>
  1. parallel = ā€œmethodsā€:Ā TestNG will run all the methods in parallel annotated with @Test annotation in separate threads. Dependent methods will also run in separate threads in the specified order.
  2. parallel = ā€œclassesā€œ:Ā TestNG will run all the test methods in the same class in the same thread but in a separate thread each Java class will get run.
  3. parallel = ā€œTestsā€œ:Ā TestNG will run all the test cases or test methods in the same <test> tag in the same thread but in different threads, each <test> tag will get run.
  4. parallel = ā€œinstancesā€œ:Ā TestNG will run all the test cases in the same instance in the same thread, but in different threads two methods on two different instances will get run.

The attribute ā€œthread-countā€ specifies no of threads should be allocated for this execution.

Letā€™s consider below example for Parallel execution of Test Methods using ā€œparallelā€ attribute for <suite> tag in testng.xml file.

Parallel Execution of Test Methods in TestNG

In below example, we will create a class with two methods and execute them in different threads.

Step 1: Launch the Eclipse

Step 2: Create a New Package (parallelTesting)

Step 3: Create a new class TestMethodparallelExecution

public class ClassOne {
@Test 
public void firstTest() 
{ 
  System.out.println(" Test method One in ClassOne"+" " +"Thread Id: " +Thread.currentThread().getId()); 
} 
@Test 
public void secondTest() 
{ 
  System.out.println(" Test method two in ClassOne"+ " " +"Thread Id: " +Thread.currentThread().getId()); 
} 

}
package parallelTesting;

import org.testng.annotations.Test;

public class ClassTwo {
@Test 
public void firstTest() 
{ 
  System.out.println(" Test method one in ClassTwo"+ " " +"Thread Id: " +Thread.currentThread().getId()); 
} 
@Test 
public void secondTest() 
{ 
  System.out.println(" Test method two in ClassTwo"+ " " +"Thread Id: " +Thread.currentThread().getId()); 
  } 

}

Now, letā€™s create a testng.xml file and configure with two attributes ā€œparallelā€ and ā€œthread-countā€ for the execution of test methods in parallel at suite level.

Here, the attribute ā€œthread-countā€ is used to pass the number of maximum threads to be created.

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

Run the above configure testng.xml file as TestNG Suite and it will display the output as

Result.PNG

From the above result you observe that two test methods have been executed in parallel in separate threads. Both test methods have taken time 0.013s for complete execution.

Parallel Execution of Test Classes in TestNG

In this scenario, we will run test cases in parallel. During execution, each java class will start and execute simultaneously in separate threads.

In below example, we will create two test classes with two methods and execute them in different threads.

Step 1: Launch the Eclipse

Step 2: Create a New Package (parallelTesting)

Step 3: Create a new class ā€œClassOneā€ with two test methods.

Now create a new testng.xml file.

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

Run the above configure testng.xml file as TestNG Suite and it will display the output in console as

method1.PNG

From 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

method2.PNG

From the above result we observe that ClassOne, and ClassTwo have executed at the same time in parallel in which firstTest() methods of both classes and secondTest() methods of both classes have the same execution time.

Parallel Execution of Tests in TestNG

In below example we will consider a program for parallel execution of tests and configure testng.xml file to the following code.

<suite thread-count = "3" 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 above configure testng.xml file as TestNG Suite and it will display the output in console as

test1.PNG

From the above 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.

test2.PNG

From the above output, both ClassOne and ClassTwo tests have run in parallel at the same time.

selenium advanced training

3 Responses

  1. Selenium is the most widely used platform for parallel test execution, and it is also used as a framework in new age test automation tools like QARA Enterprise and Ranorex.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class