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:
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:
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.