Notes
Two levels
There are two levels. Workers are parallel processes on one machine, limited by its CPU and memory; Playwright workers, pytest dash n, JUnit five parallel execution and TestNG all do this. Shards split the suite across several machines, usually several CI jobs, each running one part. You can combine them: four shards with two workers each run eight tests at a time.
- workers: parallel processes on ONE machine (CPU, memory) Playwright workers, pytest -n, JUnit 5 parallel, TestNG
- shards: the suite split across SEVERAL machines (CI jobs) --shard=1/4 ... 4/4
- both: 4 shards x 2 workers = 8 tests at a time
Playwright shards
In Playwright, the shard option takes a part and a total, like one of four. In CI, a matrix starts four jobs, one per shard. Each job uses the blob reporter, which writes a compact result file, and uploads it as an artifact with a unique name. Without fully parallel mode, Playwright splits by file, so balance how many tests each file has.
strategy:
fail-fast: false
matrix:
shard: [1/4, 2/4, 3/4, 4/4]
steps:
- run: npx playwright test
--shard=${{ matrix.shard }}
--reporter=blob
- uses: actions/upload-artifact@v4
with: { name: blob-${{ strategy.
job-index }}, path: blob-report } Merge reports
We ran the ShopRung Playwright suite from the labs in two shards. Each shard ran the login setup project plus three tests: setup is a dependency, so every shard runs it. A final CI job downloads all blob files into one folder and runs merge reports with the HTML reporter. The result is one report with all eight results, as if the suite had run on one machine.
$ npx playwright test --shard=1/2
--reporter=blob
4 passed (setup + 3 tests)
$ npx playwright test --shard=2/2
--reporter=blob
4 passed (setup + 3 tests)
$ npx playwright merge-reports
--reporter html ./all-blob
→ one report: 8 results Isolate the data
The fix is isolation. We started four ShopRungs on ports eighty two hundred to eighty two oh three, each with its own database file, and the suite picks its server from the worker id that x dist sets. Five runs out of five passed all twenty three tests. Other ways to isolate: a unique user per worker, data each test creates and owns, or a separate database schema per worker.
# xdist sets gw0, gw1, ... per worker
WORKER = os.getenv(
"PYTEST_XDIST_WORKER")
if WORKER:
BASE_URL = "http://localhost:"
+ str(8200 + int(WORKER[2:]))
# 4 ShopRungs on 8200-8203, each
# with its own SHOPRUNG_DB file
→ pytest -n 4: 23 passed, 5 of 5 Java side
In Java, JUnit five turns on parallel execution with properties in junit platform dot properties: enabled true, mode concurrent, and a dynamic strategy based on CPU cores. Tests that touch a shared resource get a resource lock annotation, so JUnit runs them one at a time. TestNG uses the parallel and thread count attributes in its XML file. Thread safety matters: one WebDriver per thread, never a shared static one.
junit.jupiter.execution.parallel
.enabled = true
junit.jupiter.execution.parallel
.mode.default = concurrent
junit.jupiter.execution.parallel
.config.strategy = dynamic
@ResourceLock("shoprung-db")
// run these one at a time
TestNG: parallel="methods"
thread-count="4" Is it faster?
Is it faster? Measure it. Our twenty three API tests took one point eight seconds in one process and about two point one with four workers: starting workers cost more than it saved. Parallel runs pay off for slow tests, like UI tests and long API flows. Balance shards by duration, not test count, and watch the slowest shard, because the job finishes only when it does.
- API suite, 23 fast tests: 1 process 1.8 s -n 4 2.1 s → start-up costs more than it saves
- parallel pays off for SLOW tests: UI, long API flows
- balance shards by duration, not by count
Common mistakes
Parallel mistakes. A global reset while other workers are running. One shared user and cart for every test. A static WebDriver shared across threads. Tests that only pass in a certain order. Shards whose reports are never merged, so nobody sees the whole result. And more workers than the machine has CPUs, which makes everything slower and flakier.
- a global reset with parallel workers
- one shared user and cart
- static WebDriver across threads
- tests that depend on order
- no merged report (4 reports nobody reads)
- more workers than CPUs
Say it like this
In the interview: first push checks down the pyramid. Then run in parallel: workers on each machine, and a CI matrix of shards whose blob reports are merged into one. Parallel runs need independent tests, so each worker or test owns its data, there are no global resets and no shared drivers. Balance shards by duration and measure the gain, because parallel has a start up cost.
- "Push checks down the pyramid
- first. Then parallel: workers per machine and a CI matrix of shards, blob reports merged into one. Tests must be independent: own data per worker or test, no global resets, no shared
- drivers. Balance shards by duration and measure, because
- parallel has a start-up cost."