</>

Technology

TestNG

Difficulty

Advanced

Interview Question

What is the difference between parallel modes in TestNG: methods, tests, classes, and instances?

TestNG parallel attribute controls the unit of parallel execution: methods run individual @Test in parallel, tests run <test> blocks, classes run entire test classes, and instances create parallel class instances.

Answer

TestNG Parallel Execution Modes

The parallel attribute in testng.xml determines WHAT runs in parallel.

1. parallel="methods" — Most Granular

Each @Test method runs in its own thread. All @Test methods across all classes run simultaneously.

XML
<suite name="Suite" parallel="methods" thread-count="5">
    <test name="Tests">
        <classes>
            <class name="tests.LoginTest"/>
            <class name="tests.CartTest"/>
        </classes>
    </test>
</suite>
  • Thread 1: LoginTest.validLogin()
  • Thread 2: LoginTest.invalidLogin()
  • Thread 3: CartTest.addItem()
  • Thread 4: CartTest.removeItem()
  • Thread 5: etc.

Requirement: Each test method must be completely independent. Use ThreadLocal<WebDriver>.

2. parallel="tests" — Most Common for Cross-Browser

Each <test> block in testng.xml runs in its own thread. Ideal for cross-browser testing.

XML
<suite name="CrossBrowserSuite" parallel="tests" thread-count="3">

    <test name="ChromeTests">
        <parameter name="browser" value="chrome"/>
        <classes><class name="tests.LoginTest"/></classes>
    </test>

    <test name="FirefoxTests">
        <parameter name="browser" value="firefox"/>
        <classes><class name="tests.LoginTest"/></classes>
    </test>

    <test name="EdgeTests">
        <parameter name="browser" value="edge"/>
        <classes><class name="tests.LoginTest"/></classes>
    </test>
</suite>

All 3 <test> blocks run simultaneously — Chrome, Firefox, Edge in parallel.

3. parallel="classes" — Class Level

Each test class gets its own thread. All methods within a class run sequentially.

XML
<suite name="Suite" parallel="classes" thread-count="3">
    <test name="Tests">
        <classes>
            <class name="tests.LoginTest"/>    <!-- Thread 1: all LoginTest methods -->
            <class name="tests.CartTest"/>     <!-- Thread 2: all CartTest methods -->
            <class name="tests.CheckoutTest"/> <!-- Thread 3: all CheckoutTest methods -->
        </classes>
    </test>
</suite>

Use when: Test methods within a class share state (same login session) but classes are independent.

4. parallel="instances" — Instance Level

Each instance of a class runs in its own thread. Used with @Factory:

XML
<suite name="Suite" parallel="instances" thread-count="3">
    <test name="Tests">
        <classes>
            <class name="factory.LoginTestFactory"/>
        </classes>
    </test>
</suite>

Use when: @Factory creates multiple class instances that should run concurrently.

Comparison Table

ModeUnit of ParallelismWithin-Class MethodsBest For
methodsEach @Test methodParallelIndependent unit tests
testsEach <test> blockSequentialCross-browser (different configs)
classesEach test classSequentialClasses sharing a session
instancesEach class instanceSequential@Factory-created instances

thread-count Setting

XML
<!-- Max threads in pool — set to (available CPUs × 2) or Grid node count -->
<suite parallel="tests" thread-count="4">

Critical: Always Use ThreadLocal for parallel="methods" or "instances"

Java
// Without ThreadLocal, threads share and overwrite the driver
private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

Follow AutomateQA

Related Topics