Lesson 4 of 9 · 25m
Parallelism and Test Isolation Without Cross-Contamination
Design workers, contexts, accounts, and shared resources so tests can run concurrently without depending on execution order.
Parallelism does not create flaky tests; it exposes tests that were never isolated. Serial execution hides shared accounts, global feature flags, fixed usernames, mutable seed data, and tests that depend on whatever the previous test happened to leave behind. The rule is simple: every test must own or lease every mutable resource it touches.
Playwright workers are separate universes
Playwright Test runs files in parallel across worker processes. Within a file, tests run in order on one worker unless you opt into fully parallel mode. This matters because module-level browser state does not cross workers, but database state absolutely does.
Browser isolation is automatic. Backend isolation is your design problem.
Namespace mutable data by worker or test
Use unique identifiers in every entity a test creates:
const runId = `${test.info().workerIndex}-${Date.now()}`;
const projectName = `Migration ${runId}`;Better yet, use the test fixture system to create and destroy resources explicitly. Fixtures make setup and teardown part of the test contract. A test should not need to know which other file created 'QA User 1'.
Avoid global mutable singletons
The classic offenders are one shared admin, one shared organization, one feature flag, one integration credential, one webhook endpoint, one payment customer, and one search index document. Any of these can convert independent tests into hidden distributed locking.
If another test is concurrently inviting a user, both tests now depend on scheduler timing. Instead, create separate tenants or use worker-scoped tenants when creation is expensive.
Lease expensive resources when creation is costly
Some resources are too slow to create per test: enterprises, Kubernetes namespaces, payment processor subaccounts, or large imported datasets. Use a pool with explicit leases. A simple implementation is a database table or test service that assigns unused fixtures and marks them in use. The lease must include worker identity and expiration so crashed CI jobs do not deadlock the suite.
fixture_pool
id
resource_key
leased_by
lease_expires_at
reset_requiredDo not implement this with files in a shared checkout unless you enjoy stale locks.
Order independence is the acceptance test
Run tests repeatedly, shuffled where possible, and with different worker counts:
npx playwright test --workers=1
npx playwright test --workers=4 --repeat-each=3A test that passes only at one worker count is not isolated. A test that passes only when the whole suite runs is worse: it is consuming undocumented state.
Field notes
- Unique names alone are not enough if tests mutate a shared singleton or enforce global uniqueness constraints.
- Teardown must be idempotent; interrupted CI means cleanup will sometimes run against partial state.
- Shared read-only fixtures are fine. Shared mutable fixtures need ownership or locking semantics.
- A fixed
adminsession reused by all workers is a concurrency bug even if each browser context is isolated. - When failure appears only under load, inspect application queues and background workers; tests may be synchronized but backend jobs are not.
Was this lesson helpful?