All IT Courses 50% Off
Selenium Tutorials

Parallel Test Execution in Selenium using TestNG

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

package parallelTesting;

import org.testng.annotations.Test;

public class TestMethodparallelExecution {
@Test 
public void firstTest() 
{ 
  System.out.println("Test Method One"); 
} 
@Test 
public void secondTest() 
{ 
  System.out.println("Test Method Two"); 
  } 

}

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.

package parallelTesting;

import org.testng.annotations.Test;

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 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
Facebook Comments
Tags

One Comment

  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.

Related Articles

Back to top button
Close
Close

Get Selenium Training Online
Course Now at 50%OFF!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys will use this information to be in touch with you for providing updates.