TestNG allows running test methods in parallel, which can significantly reduce overall test execution time, especially in large test suites.
Parallel execution is configured using the parallel and thread-count attributes inside the testng.xml file.
Supported parallel Modes in TestNG
The parallel attribute inside the tag accepts the following values:
Mode
|
Description
|
methods
|
Each test method runs in its own thread. Even dependent methods run in separate threads but maintain execution order.
|
classes
|
All methods inside a class run in the same thread, but each class runs in a separate thread.
|
tests
|
Each tag (in testng.xml) runs in a separate thread, but test methods within a share the same thread.
|
instances
|
Each test class instance runs in a different thread. Methods within the same instance run sequentially.
|
Example: Parallel Execution by Methods
Java Class (TestngTest.java)
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestngTest {
@Parameters({ "TestFor" })
@Test
public void login(String TestFor) {
System.out.println("Automated test for - " + TestFor);
Reporter.log("Login test executed.");
}
@Parameters({ "TestFor" })
@Test
public void registration(String TestFor) {
System.out.println("Registration script started...");
Reporter.log("Registration test executed.");
}
@Parameters({ "TestFor" })
@Test
public void summary(String TestFor) {
System.out.println("Summary script started...");
Reporter.log("Summary test executed.");
Assert.fail("Fail at Summary");
}
}
testng.xml Configuration (Parallel by methods)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" thread-count="3">
<parameter name="TestFor" value="Web" />
<test name="Annotated Test">
<classes>
<class name="org.example.TestngTest" />
</classes>
</test>
</suite>
In this case:
All test methods (login, registration, summary) will run concurrently in separate threads.
Example: Parallel Execution by Classes
testng.xml (Parallel by classes)
<?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">
<parameter name="TestFor" value="Web" />
<test name="Annotated Test">
<classes>
<class name="org.example.TestngTest" />
<class name="org.example.TestngLoginTest" />
</classes>
</test>
</suite>
Here:
Each class (TestngTest, TestngLoginTest) will be executed in a separate thread, while methods within a class run sequentially.
Example: Parallel Execution by Tests
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Test 1">
<parameter name="TestFor" value="Web" />
<classes>
<class name="org.example.TestngTest" />
</classes>
</test>
<test name="Test 2">
<parameter name="TestFor" value="Web" />
<classes>
<class name="org.example.TestngLoginTest" />
</classes>
</test>
</suite>
In this setup:
Each runs in a separate thread
All classes and methods under that are executed sequentially within the thread
Notes
The value of thread-count determines the maximum number of threads that can run in parallel.
Avoid running dependent tests in parallel unless properly isolated.
Use assertions, logging, and synchronized blocks carefully in multi-threaded tests.