Parallel Test Execution Using TestNG
Why Run Tests in Parallel?
As a test suite grows, running tests sequentially can take a long time. TestNG supports running tests in parallel to significantly reduce total execution time.
Configuring Parallel Execution
<suite name="Suite" parallel="methods" thread-count="4">
...
</suite>
The parallel attribute can be set to run tests in parallel at the method, class, or test level, while thread-count controls how many threads run simultaneously.
Things to Watch For
Parallel execution requires tests to be independent of each other (no shared mutable state), or race conditions can cause unreliable, hard-to-debug failures.
Choosing the Right Parallel Mode
| Value | What runs at the same time | Typical use |
|---|---|---|
methods | Individual test methods, even inside one class | Fast feedback when every method is fully independent |
classes | Whole classes; methods inside a class run in order on one thread | UI suites where each class owns its own browser |
tests | Each <test> block in testng.xml | Running the same suite on different browsers |
instances | Methods of different instances of the same class | Factory-created test objects |
Cross-Browser Run Example
A common setup runs one <test> block per browser, passing the browser name as a parameter.
<suite name="CrossBrowser" parallel="tests" thread-count="2">
<test name="Chrome">
<parameter name="browser" value="chrome"/>
<classes><class name="tests.LoginTest"/></classes>
</test>
<test name="Firefox">
<parameter name="browser" value="firefox"/>
<classes><class name="tests.LoginTest"/></classes>
</test>
</suite>
Keeping WebDriver Thread-Safe
The biggest source of flaky parallel runs is a single static WebDriver shared by all threads. Wrap it in ThreadLocal so each thread gets its own browser.
public class DriverFactory {
private static final ThreadLocal<WebDriver> DRIVER = new ThreadLocal<>();
public static void init(String browser) {
DRIVER.set(browser.equals("firefox") ? new FirefoxDriver() : new ChromeDriver());
}
public static WebDriver get() { return DRIVER.get(); }
public static void quit() {
DRIVER.get().quit();
DRIVER.remove(); // avoid leaking drivers between tests
}
}
public class BaseTest {
@Parameters("browser")
@BeforeMethod
public void setUp(@Optional("chrome") String browser) { DriverFactory.init(browser); }
@AfterMethod(alwaysRun = true)
public void tearDown() { DriverFactory.quit(); }
}
Other Ways TestNG Runs Work in Parallel
- Data providers:
@DataProvider(parallel = true)runs each data row on its own thread. Control the pool withdata-provider-thread-counton the suite. - Repeated calls:
@Test(invocationCount = 10, threadPoolSize = 3)calls one method many times across several threads, handy for simple concurrency checks. - Maven Surefire: point Surefire at the suite file so
mvn testuses the same settings in CI.
When choosing between these, remember that suite-level settings apply to everything in the file, while annotation settings affect one method only. Begin with parallel="classes" for UI suites: it is the easiest mode to make stable, because methods within a class still share a predictable order. Move to methods only once every test sets up and cleans up its own data.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles>
</configuration>
</plugin>
Tuning and Troubleshooting
- Start with a thread count close to the number of CPU cores or grid slots, then measure; more threads do not always mean faster runs.
- Give each test its own data, such as a unique user, so tests do not edit the same record.
- Avoid
dependsOnMethodschains in parallel suites; they force ordering and reduce the benefit. - Make reports and logs thread-aware, for example by including the thread or test name in each log line.
- Watch out for page objects or helpers stored in static fields; they hold a reference to one thread's driver and break other threads.
- If failures appear only in parallel runs, look first for static fields and shared files.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.