Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 16, 2016

Parallel Execution of Classes In TestNG





Parallel execution of classes in TestNG will discuss about ability of executing the classes in parallel based on the test suite configuration. Different thread will start simultaneously and the classes will be executed in those threads. This will reduce the execution time while running the multiple classes as a suite. You can achieve this functionality in different ways using the testng.xml file configuration.

In this, we will take 2 classes and each class will contain one test method with @BeforeMethod and @AfterMethod annotation. As @BeforeMethod will execute before executing the test method and @AfterMethod will execute after executing the test method.

Executing Classes in Parallel:

Below is the sample code for first Class:

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class classOne
{
    @BeforeMethod
    public void beforeMethod()
    {
        System.out.println("I am in beforeMetod - classOne");
    }
    
    @Test
    public void testOne()
    {
        System.out.println("I am in testOne method - classOne");
    }
    
    @AfterMethod
    public void afterMethod()
    {
        System.out.println("I am in afterMetod - classOne");
    }
}

Create one more class with the same set of tests.

Below is the sample code for second class:

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class classTwo
{
    @BeforeMethod
    public void beforeMethod()
    {
        System.out.println("I am in beforeMetod - classTwo");
    }
    
    @Test
    public void testOne()
    {
        System.out.println("I am in testOne method - classTwo");
    }
    
    @AfterMethod
    public void afterMethod()
    {
        System.out.println("I am in afterMetod - classTwo");
    }
}

Now create a testng.xml file to execute the above classes as parallel.

Below is the sample code:

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





In the above XML file, we have created a suite and then we have assigned some attributes to that suite. One is “parallel” this attribute accepts a value called “classes” and second is “thread-count” and it accepts value as integers. When you execute this xml file then it executes the classes in parallel mode as we have configured that in the xml file. It will create 2 threads to execute the classes in parallel.

Now we will see the execution with and without parallel then we can come to know the difference:

Without Parallel execution:

Just remove the parallel and thread-count attributes from the testng.xml file and execute the same then we can find the below output:

1

With Parallel execution:

Add Parallel and thread-count attributes to the testng.xml file and then execute the same then we can find the below output:

2

From the above outputs, we can observe that when you use parallel and thread-count attributes both the classes started parallelly and executed. This way we can configure the testng.xml file to execute the classes parallelly. By using this mechanism we can reduce the execution time while executing the test suite which will contain more number of classes.

Please watch the you tube video for better understanding.



Share this post: on Twitter on Facebook

Parallel Execution of Methods In TestNG TestNG Assertions

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025