Quick revision
The short answer
The parallel attribute in testng.xml decides what TestNG runs at the same time: with methods, any test methods can run in separate threads, even methods of the same class instance; with classes, each class runs in one thread and different classes run in parallel; with tests, each <test> element gets its own thread; and with instances, methods of the same object share a thread while different instances run in parallel. thread-count sets the size of the thread pool, and data-provider-thread-count is a separate pool for parallel data providers.
What to remember
In this answer, we saw that a testng.xml suite contains <test> elements, which contain classes, and that TestNG uses one instance per class by default. parallel = methods runs any methods concurrently, classes keeps each class on one thread, tests gives each <test> element a thread, and instances gives each object from a @Factory its own thread. thread-count sizes the pool, data-provider-thread-count sizes a separate pool for parallel data providers, and parallel methods race on instance fields because they all share the same object.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The parallel attribute in testng.xml decides what TestNG runs at the same time: with methods, any test methods can run in separate threads, even methods of the same class instance; with classes, each class runs in one thread and different classes run in parallel; with tests, each <test> element gets its own thread; and with instances, methods of the same object share a thread while different instances run in parallel. thread-count sets the size of the thread pool, and data-provider-thread-count is a separate pool for parallel data providers.
Why this question is asked
Parallel execution is the cheapest way to make a slow TestNG suite faster, and also the quickest way to make a stable suite flaky. Which of the two we get depends on choosing the right parallel mode for how our tests share state. Interviewers ask about the modes because the differences look small in the XML but are large in behaviour, and because one detail, that TestNG creates a single instance per test class, explains most of the race conditions people hit. In this answer, we will recap how TestNG organises a suite, go through each mode with an example, look at thread-count and data provider threads, and then see why parallel = methods breaks tests that use instance fields.
1A Quick Recap of a TestNG Suite
A testng.xml file describes a suite. The <suite> element contains one or more <test> elements, which are not test methods but named groups of classes, for example a test called checkout that lists three classes. Each class contains test methods annotated with @Test.
One more fact matters for everything below: by default, TestNG creates exactly one instance of each test class and runs all its methods on that instance. JUnit Jupiter, by contrast, creates a new instance for each test method by default. So in TestNG, a field set by one test method is visible to the next one, and to any method running at the same moment on another thread.
The parallel and thread-count attributes can be set on <suite> and, for some modes, on individual <test> elements. Leaving parallel out, or setting it to none, runs everything on one thread.
2The Four Parallel Modes
With parallel = "methods", TestNG schedules each test method as its own unit of work. Two methods of CartTest can run at the same time on different threads, on the same CartTest instance. This gives the most parallelism and the most risk.
With parallel = "classes", all methods of one class run on the same thread, one after the other, while different classes run in parallel. CartTest and PaymentTest run at the same time, but the methods inside each stay sequential. This is the usual safe starting point, because a class's fields are only touched by one thread.
With parallel = "tests", each <test> element runs on its own thread, and everything inside one <test> runs sequentially. In the suite above, checkout and search would run in parallel. This is useful when each <test> targets a different browser or environment.
With parallel = "instances", methods of the same instance run on the same thread, and different instances run in parallel. This only matters when there are several instances of a class, which we get with @Factory, a TestNG annotation for a method that returns several test objects, for example one per customer type. Each instance then gets its own thread for its methods.
| Mode | Unit of parallel work | Safe with instance fields? |
|---|---|---|
| methods | Each test method | No: methods share one instance |
| classes | Each class, methods in order | Yes, within the class |
| tests | Each <test> element | Yes, within the <test> |
| instances | Each object from @Factory | Yes, within the instance |
| none | Nothing, one thread | Yes |
3What thread-count Controls
thread-count is the size of the thread pool TestNG uses for the chosen mode. With parallel = "classes" and thread-count = "4", at most four classes run at once; the fifth waits for a free thread. The default is 5. More threads only help when tests spend time waiting, for a database, a browser or the network, and when the environment can serve that many tests at once.
Data providers have their own setting. A data provider marked @DataProvider(parallel = true) lets TestNG run the rows of that provider at the same time, using a separate pool whose size is data-provider-thread-count, 10 by default. That pool is independent of thread-count, so a suite with thread-count = 4 and two parallel data providers can easily have more than 4 tests running at once.
Notice that parallel = true is set on the provider, not on the test. Without it, the rows run one after the other even in a parallel suite.
4Why parallel = methods Causes Races
Because there is one instance per class, parallel methods share every field of that instance. Here is a class that works perfectly when run sequentially.
The reason is that @BeforeMethod for the second test replaces the field while the first test is still using it, and both end up adding to the same Cart. The fix is either to switch to parallel = "classes", or to keep per-test state out of instance fields: use local variables, or a ThreadLocal<Cart> created in @BeforeMethod and removed in @AfterMethod, so each thread has its own copy.
5Follow-up questions
Which mode would you start with on an existing suite?
parallel = "classes" with a small thread-count such as 2 or 4. It keeps the methods of each class sequential, so instance fields stay safe, and it usually gives most of the speed-up. If it stays stable for a while, we can try methods on classes that we know keep no shared fields.
Do configuration methods like @BeforeClass run in parallel too?
They follow the mode. With classes, a class's @BeforeClass runs on that class's thread before its methods. With methods, @BeforeMethod runs on the same thread as the test it prepares. @BeforeSuite and @BeforeTest run before the parallel work for the suite or <test> begins, which is why they are the right place for shared, read-only setup.
Can individual tests opt out of parallel execution?
Yes. @Test(singleThreaded = true) on a class makes all its methods run on one thread even when the suite uses parallel = methods. That is a useful escape hatch for a class that cannot be made thread-safe yet.
6Conclusion
In this answer, we saw that a testng.xml suite contains <test> elements, which contain classes, and that TestNG uses one instance per class by default. parallel = methods runs any methods concurrently, classes keeps each class on one thread, tests gives each <test> element a thread, and instances gives each object from a @Factory its own thread. thread-count sizes the pool, data-provider-thread-count sizes a separate pool for parallel data providers, and parallel methods race on instance fields because they all share the same object.
Open with
The parallel attribute says what TestNG runs concurrently. With methods, every test method is a separate unit, so two methods of the same class can run at the same time on different threads, on the same object.
Then, if they want more
With classes, each class runs on one thread with its methods in sequence, and different classes run in parallel, which is the safe default for most suites. With tests, each test element in testng.xml, which is a named group of classes, gets its own thread, useful when each one targets a different browser.
With instances, methods of one object share a thread and different objects run in parallel, which matters when a @Factory creates several instances of a class. thread-count is the pool size, five by default, so it caps how many units run at once.
Data providers marked parallel equals true use a separate pool sized by data-provider-thread-count, ten by default, so the total concurrency can exceed thread-count. The trap is that TestNG creates one instance per class, unlike JUnit Jupiter's instance per method.
So with parallel equals methods, a field set in @BeforeMethod is shared by all methods running at that moment, and one test overwrites another's state. I would either use classes mode or keep per-test state in local variables or a ThreadLocal.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do parallel = "methods", "classes", "tests" and "instances" mean in testng.xml, and...": https://scrivio-interview-library.netlify.app/java/testng/3272a5a4c0/