The arithmetic of parallel testing is seductive. A regression suite of 600 UI tests averaging 30 seconds each takes 5 hours when run one at a time. Spread it across 10 parallel workers and, in theory, it finishes in 30 minutes. Teams flip the parallel switch in TestNG, JUnit 5 or pytest-xdist, and the first run produces a mess: tests that pass alone fail together, screenshots show the wrong page, and a test logged in as one user suddenly sees another user’s cart.

None of this is random. It is the same class of problem developers meet in multithreaded code: two units of work touching the same mutable state without coordination. The fix is not more waits or retries. It is finding every piece of shared state and either removing it or giving each worker its own copy.

Threads versus processes: know which you are using

Parallel runners work in one of two ways. Some run tests on multiple threads inside one process, as TestNG and JUnit 5 do by default. Threads share memory, so any static field, singleton or class-level variable is visible to all of them at once. Others run tests in separate processes, as pytest-xdist and Playwright Test’s workers do. Processes do not share memory, so in-memory collisions mostly disappear, but they can still collide on anything outside the process: files, ports, databases, and the application’s own state.

Knowing which model your runner uses tells you where to look first. With threads, start with your code. With processes, start with your data and environment.

The classic culprit: one static driver

Many Selenium frameworks begin life with a base class holding a static WebDriver field. It works perfectly in a sequential run. Under thread-level parallelism, every thread reads and writes that single field. Thread A creates a browser and stores it; thread B creates another and overwrites the field; thread A’s next click now goes to thread B’s browser. Worse, when one test quits the driver in its teardown, it closes a browser another test is still using.

The standard fix in Java is ThreadLocal, a holder that gives each thread its own independent value. A driver manager stores the driver in a ThreadLocal, creates it in setup, hands it out through a getter, and in teardown both quits the browser and calls remove on the holder. That last step matters: test runners reuse threads from a pool, and a stale reference left behind can leak into the next test that the thread picks up, or keep memory alive longer than needed.

The same idea applies to anything else stored statically per test: the current page objects, the logged-in user, a soft-assertion collector, an extent report node. If it describes “the current test”, it must not be a plain static.

Shared state you do not see at first

Once the driver is fixed, the next wave of failures usually comes from less obvious places:

  • Non-thread-safe utilities. In Java, SimpleDateFormat is not thread-safe, and a shared instance used to generate timestamps can produce corrupted dates. A plain HashMap used as a shared cache can misbehave under concurrent writes. Use immutable or thread-safe alternatives such as DateTimeFormatter and ConcurrentHashMap, or keep such objects local to each test.
  • Files. Two tests downloading an invoice to the same folder with the same name will read each other’s files. Give each worker a unique download directory, and name screenshots and logs with the test name and a unique suffix.
  • Ports and local services. Tests that start a mock server on a fixed port will fight for it. Let the operating system assign a free port and pass it to the test.
  • Reporting libraries. Some reporting setups were written assuming one test at a time. Check that your report attaches logs to the correct test under parallelism.

The hardest shared state: the application’s data

Even with perfect framework code, tests share one thing they cannot escape: the system under test. Imagine an e-commerce suite where every test logs in as testuser01. Test A adds a phone to the cart and checks the total is ₹15,999. Test B, running at the same moment, empties the cart. Test A fails, and rerunning it alone passes. This failure is entirely about data, and no amount of framework threading will fix it.

The durable approach is data isolation: each test creates the data it needs, ideally through an API, uses it, and does not depend on anything another test touches. Unique users, unique orders and unique names (with a short random or timestamp suffix) remove most collisions. Where creating fresh data is too slow or impossible, allocate a pool of accounts and lease one account to one worker at a time, returning it after the test.

Some tests genuinely cannot run in parallel: ones that change global settings such as a tax rate or feature flag, or that check a count across the whole system (“the admin dashboard shows 42 pending orders”). Mark these explicitly and run them in a separate, serial group. Pretending they are parallel-safe guarantees intermittent failures.

Order dependence: the hidden assumption

Sequential suites often contain tests that only pass because another test ran first: one creates a customer, the next edits “the latest customer”. Parallel runs scramble the order and expose this. A good diagnostic, even before you go parallel, is to run the suite in random order. Pytest has plugins for this and JUnit 5 supports a random method orderer. Every test that fails under random order has a dependency to remove.

How many workers is right?

The ideal 30-minute figure from the opening assumes perfect scaling, which never happens. Each browser needs CPU and memory; on a CI agent with 4 cores, ten Chrome instances will slow each other down and introduce timing failures that look like flakiness. The application under test may also be the limit: a shared QA environment sized for a handful of users can buckle when 20 workers hit it at once, and you end up load testing it by accident.

The practical method is to increase workers in steps, watching total run time, failure rate and the resource usage of both the runner and the environment. Run time usually drops steeply at first and then flattens. Going past that point adds failures without saving time. A single very long test also sets a floor on total duration, which is a good argument for splitting long end-to-end journeys.

A short checklist before turning on parallelism

  1. Does the suite pass in random order when run sequentially?
  2. Is every per-test object (driver, page, user, report node) held per thread or per worker?
  3. Do tests create or lease their own data instead of sharing fixed accounts?
  4. Are files, ports and directories unique per worker?
  5. Are global-state tests tagged and run serially?

A suite that answers yes to all five will usually scale cleanly. One that does not will produce failures that look random but are perfectly predictable once you know what is being shared.

Written by the RP Strativa Team. Suite sizes, timings and prices in the examples are illustrative.

Leave a Reply

Trending