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:
- <suite name = “Parallel Test Suite” parallel = “methods” thread-count = “4”>
- <suite name = “Parallel Test Suite” parallel = “classes” thread-count = “4”>
- <suite name = “Parallel Test Suite” parallel = “tests” thread-count = “4”>
- <suite name = “Parallel Test Suite” parallel = “instances” thread-count = “4”>
- 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.
- 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.
- 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.
- 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
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
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
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
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.
From the above output, both ClassOne and ClassTwo tests have run in parallel at the same time.
3 Responses
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.