skip to content

In a TestNG suite file, what does each parallel value (methods, classes, tests, instances) keep on one thread, and what does thread-count size?

level: middleimportance: must knowfreq 62%

answer

  1. the unit that stays on one thread
  2. a test element is a batch of classes
  3. one class instance by default
  4. pool size, default five

basics

~20 s

TestNG's parallel attribute picks the unit kept on one thread: methods keeps nothing together, classes keeps each class's methods, tests keeps each <test> element, instances keeps each object's methods. thread-count sizes the pool, default 5.

solid answer

~40 s

The default is `parallel="none"`. With `methods`, any test method can run on any pool thread, though dependent methods still wait for what they depend on. With `classes`, all methods of one class run on one thread and different classes run concurrently. With `tests`, every method inside one `<test>` element runs on the same thread and the `<test>` elements run concurrently. With `instances`, methods of the same object share a thread and different instances, typically from a `@Factory`, run concurrently. `thread-count` (default 5) sizes the pool: under `tests` it is the suite pool that runs `<test>` elements; under the other modes each `<test>` gets its own pool and the `<test>` elements run one after another. Because TestNG creates one instance per class by default, `methods` mode means concurrent methods share that instance's fields.

code

xml · 11 lines
xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="cross-browser" parallel="tests" thread-count="2">
  <test name="chrome">
    <parameter name="browser" value="chrome"/>
    <classes><class name="com.shop.CheckoutTest"/></classes>
  </test>
  <test name="firefox">
    <parameter name="browser" value="firefox"/>
    <classes><class name="com.shop.CheckoutTest"/></classes>
  </test>
</suite>

go deeper

for a junior

Recall the four values and that none is the default; know thread-count sizes the pool and defaults to 5.

for a middle

Explain what each mode keeps on one thread, that tests means the <test> element, and how the pool is built per <test> except under tests.

for a senior

Predict the failure a mode change exposes: one shared instance under methods, a typo that turns parallelism off, a pool smaller than the operator expected.

for a principal

Treat the mode as a team default with a cost: which unit of state the suite's classes are built around, and who approves a <test>-level override.

## The parallel attribute In a TestNG suite file, the `parallel` attribute on `<suite>` (or on an individual `<test>`, which overrides it) chooses **which unit of work TestNG keeps together on one thread**. Its values, checked against the 7.12 `XmlSuite.ParallelMode` enum, are `none` (the default), `methods`, `classes`, `tests` and `instances`. The DTD still accepts the legacy values `true` and `false`; TestNG maps them to `methods` and `none` and logs that they are no longer valid. `thread-count` gives the **size of the thread pool**, with a default of `5`. A `<test>` can set its own `thread-count`, which overrides the suite value for that `<test>`. ## What each mode keeps on one thread | Mode | Kept on one thread | Runs concurrently | |---|---|---| | `none` | everything | nothing | | `methods` | nothing is guaranteed | any test methods, across classes | | `classes` | all methods of one class | different classes | | `tests` | all methods in one `<test>` element | different `<test>` elements | | `instances` | all methods of one object | different instances of a class | Details that matter: - **`methods`**: test methods are dispatched to pool threads individually. The documentation notes that dependent methods also run on separate threads but still respect the declared order, so a method declared with `dependsOnMethods` waits for its upstream to finish. - **`classes`**: all methods of one class share one thread, so that class's own setup and its test methods do not race each other, while two classes run side by side. - **`tests`**: the documentation's stated purpose is to put classes that are not thread-safe into one `<test>` and still use many threads across `<test>` elements. Inside a `<test>`, methods run sequentially unless that `<test>` sets its own `parallel` value. - **`instances`**: meaningful when a class has several instances, usually created by a `@Factory`. Methods on the same object stay together; two objects run on different threads. With one instance per class it behaves much like `classes`. ## How thread-count is applied The pool is not built the same way in every mode, which the 7.12 source makes explicit: 1. **`parallel="tests"`** - the suite runs its `<test>` elements through a pool of the **suite's** `thread-count`. Two browsers in two `<test>` elements with `thread-count="2"` run side by side. 2. **`methods`, `classes`, `instances`** - each `<test>` element builds **its own pool** of its `thread-count`, and the `<test>` elements themselves run **one after another** by default. 3. **Several suite files** run in parallel through the suite thread pool size (the command-line `-suitethreadpoolsize` option), not through `thread-count`. 4. **Parallel data providers** use a separate pool sized by `data-provider-thread-count`, default `10`. So `parallel="methods" thread-count="8"` with three `<test>` elements gives at most eight concurrent methods at a time, `<test>` by `<test>`; it does not give 24. ## What concurrent methods share By default TestNG creates **one instance of each test class** for a `<test>`, unless a `@Factory` creates more. Under `classes` that single instance is used by one thread at a time. Under `methods`, several threads call methods on the **same object at once**, so a field written by a `@BeforeMethod` on one thread can be overwritten by another before the first test reads it. That is the most common failure after switching a Selenium suite to `methods`: a `driver` field shared by concurrent methods. JUnit Jupiter differs here, because it creates a new test instance per test method by default; TestNG does not. ## Typos and validation `parallel` values are checked by the DTD only when the file declares the TestNG DOCTYPE. Without it, an unrecognised value such as `"method"` is mapped to `none` in the source, so the run goes serial with no error about the value, only the generic warning recommending the DOCTYPE. Declaring the DOCTYPE turns that mistake into a parse error. ## A minimal example ```xml <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="regression" parallel="classes" thread-count="4"> <test name="api"> <classes> <class name="com.shop.OrdersApiTest"/> <class name="com.shop.PaymentsApiTest"/> </classes> </test> </suite> ``` Both classes run at once, each on its own thread, and each class's methods run one after another on that thread. ## Quick rules to say in an interview - `tests` groups by `<test>` element, never by `@Test` method. - `methods` is the only mode where two methods of the same class instance can overlap. - `thread-count` is per `<test>` except under `tests`, where it is the suite's pool. - Parallelism is off until `parallel` is set; the default is `none`.

  • After switching a TestNG Selenium suite from classes to methods, tests drive each other's browsers. Why?
    TestNG creates one instance per test class by default. Under `classes`, one thread uses that instance at a time; under `methods`, several threads call methods on the same object concurrently, so a `driver` field set in `@BeforeMethod` by one thread is overwritten by another. The mode change exposed shared instance state that `classes` had kept on one thread.
  • With parallel="methods" and thread-count="5" at suite level and three <test> elements, how many methods can run at once?
    At most five. Under `methods`, each `<test>` element gets its own pool of its `thread-count`, and the `<test>` elements run one after another by default, so the three pools are not active simultaneously. Only `parallel="tests"` runs `<test>` elements concurrently.
  • In a TestNG suite file, what happens to parallel="method" (singular)?
    With the TestNG DOCTYPE declared, the DTD rejects the value and the file fails to parse. Without it, TestNG's parser does not recognise the value and maps it to `none`, so the suite runs serially with no error about the value. That is a reason to require the DOCTYPE in every suite file.

  • Says parallel="tests" runs each @Test method on its own thread
  • Believes TestNG creates a fresh test-class instance per method, as JUnit Jupiter does
  • Thinks thread-count multiplies across every <test> element under parallel="methods"
  • Assumes the default parallel mode is methods rather than none
  • Expects thread-count to make several suite files run concurrently

Assumes TestNG 7.12; the mode list, the defaults of none and 5, and the per-test pooling were checked against the 7.12.0 source.