#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
TestNG & Maven

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

ValueWhat runs at the same timeTypical use
methodsIndividual test methods, even inside one classFast feedback when every method is fully independent
classesWhole classes; methods inside a class run in order on one threadUI suites where each class owns its own browser
testsEach <test> block in testng.xmlRunning the same suite on different browsers
instancesMethods of different instances of the same classFactory-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 with data-provider-thread-count on 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 test uses 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 dependsOnMethods chains 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.

Explore Course