Parallelism, workers and sharding
Tests run in workers, separate Node processes: files in parallel by default, individual tests with fullyParallel. After a failure the worker is closed and a new one starts, but that resets the browser, not the server state, so tests need their own data, such as a user created in a fixture. Sharding splits a run across machines, and shard reports are merged with the blob reporter and merge-reports.
Playwright · Config and CI
Tests are green with one worker and fail with eight. Who is stealing whose data?
On a laptop the tests run in four workers and pass. On CI an unstable failure appears: "wrong password" in a test that worked yesterday. The cause is not the browser: two tests logged in with the same account, and one of them was changing the password. Parallelism does not break tests, it reveals which of them share data. This lesson explains how Playwright distributes tests across workers, what happens after a failure, and how to make tests independent.
How it works and why. A worker is a separate Node process with its own browser. The default count is half of the logical cores; it is set through workers (a number or a percentage such as '50%') or the --workers flag. Workers share no memory, so tests in different workers share neither variables nor pages. The default unit of work is a file: different files run in parallel, while the tests of one file run one after another in a single worker. With fullyParallel: true the unit becomes a single test.
What counts as a unit of work
By default
Files run in parallel, the tests of a file one after another. One big file occupies one worker while the rest sit idle.
fullyParallel: true
Single tests run in parallel, even from one file. The load is more even, but tests must be independent.
A separate safety rule applies: when a test fails, the worker is shut down and the following tests start in a new one. That way browser state, cookies and open connections do not leak from a broken test into the next one.
What happens to a worker after a failure
The last sentence of the caption matters: a fresh process heals browser state but not server state. If tests share an account or a database record, isolation ends where the network begins.
Example: a shared account. A typical cause of an "unstable" failure:
// before: one account for all tests
const USER = { email: 'qa@test.app', password: 'Secret-1' };
test('change password', async ({ page }) => {
await login(page, USER);
await changePassword(page, 'New-2'); // until the password is restored, logging in with the old one fails
await changePassword(page, USER.password);
});
test('profile opens', async ({ page }) => {
await login(page, USER); // sometimes: "Wrong password"
await expect(page.getByRole('heading', { name: 'Profile' })).toBeVisible();
});
With --workers=1 the tests run one after another, and the order happens to save them. To prove the cause is parallelism and not a locator, compare two runs: npx playwright test --workers=1 (green) and npx playwright test --workers=4 --repeat-each=5 (red). The cure: each test gets its own user. A fixture creates it through the API and removes it after the test, even if the test failed:
// after
import { test as base } from '@playwright/test';
import { randomUUID } from 'node:crypto';
export const test = base.extend<{ account: { email: string; password: string } }>({
account: async ({ request }, use) => {
const email = 'qa-' + randomUUID() + '@test.app';
const password = 'Secret-' + randomUUID();
const res = await request.post('/api/users', { data: { email, password } });
const { id } = await res.json();
await use({ email, password });
await request.delete('/api/users/' + id);
},
});
A UUID is more reliable for unique values. Worker indexes exist too: testInfo.parallelIndex is a stable slot from 0 to workers - 1 (useful when, say, one prepared account falls to each worker), while testInfo.workerIndex changes when a worker is restarted after a failure.
Describe modes. test.describe.configure({ mode: 'serial' }) runs a block's tests one after another in one worker: if one fails, the rest are skipped, and on a retry the block starts from the first test. The 'parallel' mode, on the contrary, parallelises tests in one file even when fullyParallel is off. Keep serial for steps that really do not exist on their own, and even then carefully: a variable between tests (let orderId) works only because both tests are in one worker, and running only the second test with -g will not reproduce the failure.
Sharding. This is one more level of parallelism: the run is split between machines with npx playwright test --shard=1/4. Without fullyParallel the split goes by files, so uneven files give a skew between shards; with it, tests are distributed individually. Shard reports are combined with the blob reporter and merge-reports; the full pipeline is covered in the CI lesson.
Limits and alternatives. Data isolation is not free: creating a user through the API adds a request per test. A cheaper scheme is one account per worker through a worker fixture (it lives as long as the worker does), but then tests of one worker share the account again and must not change its state. If a resource cannot be multiplied (a single global flag, a licence slot), those tests go to a separate --workers=1 run selected by a tag. In Cypress, parallelism means separate machines and a paid or self-hosted orchestrator, while inside one spec the tests always run one after another; in Selenium parallelism comes from TestNG, JUnit or pytest-xdist, and data isolation is also yours to solve.
Common mistakes.
- A shared account or DB record: with
--workers=1the tests are green, in parallel they fail every other run, and the stack trace explains nothing. - Depending on the order of tests in a file without
serial: it breaks with--shard,-gorfullyParallel. - Too many workers on a weak runner: browsers compete for CPU, timeouts fire, and the failure looks like an application bug.
- Cleaning data at the end of a test instead of in a fixture after
use: on a failure it does not run and litters the database. - A module-level variable as shared state between tests: each worker has its own process and its own copy of the variable.
Check yourself
Two tests in one file, fullyParallel is not enabled, four workers. One changes a password, the other logs in with the same account. Can they conflict?
Answer
No, while fullyParallel is off: tests of one file run one after another in one worker. But turn on fullyParallel, add sharding, or run these files with other tests of the account, and the conflict appears. The order dependency is hidden, and it should be removed with a separate account.
Test 2 failed, and then test 3 failed too, but only on CI. Why do new workers not guarantee a clean start?
Answer
A new worker gives a fresh browser and process but does not clean the server. If test 2 left a DB record or changed an account, test 3 will see that state. So data is created in a fixture and removed after use.
What will a run with --workers=1 and one with --workers=4 --repeat-each=5 show for a test with shared data?
Answer
The first is most likely green, because the execution order is deterministic. The second raises the chance of workers colliding and reproduces the unstable failure. If the behaviour differs, the cause is shared state, not a locator or a wait.
fullyParallel tests in a file too, so each test must work with its own data. Middle/Senior: A worker is a separate process; after a failed test it is shut down, but that does not clean server state, so I create unique users in a fixture through the API and delete them after use. I prove instability from shared data by comparing --workers=1 with --workers=4 --repeat-each. I take serial rarely, and for sharding I turn on fullyParallel and merge blob reports.