Handling Shared Resources and Test Data

๐Ÿ“‹ Table of Contents โ–พ
  1. Designing Parallel-Safe Test Data
  2. Common Mistakes

Parallel execution puts pressure on how you design shared resources like users, test data and environments. If multiple tests compete for the same state, you can get intermittent failures that are hard to reproduce.

Designing Parallel-Safe Test Data

To keep tests independent, you need data strategies that avoid collisions. Common patterns include unique identifiers per test, per-worker data partitions and ephemeral test fixtures that clean up after themselves.

// example of generating unique data per test
import { test, expect } from '@playwright/test';

-test('creates a unique user per test', async ({ page }, testInfo) => {
+test('creates a unique user per test', async ({ page }, testInfo) => {
  const uniqueEmail = `user+${testInfo.testId}@example.com`;

  await page.goto('https://demo.myshop.com/register');
  await page.getByLabel('Email').fill(uniqueEmail);
  await page.getByLabel('Password').fill('SuperSecret123');
  await page.getByRole('button', { name: 'Sign up' }).click();

  await expect(page.getByText('Welcome')).toBeVisible();
});
Note: Using the test ID as part of unique keys guarantees that concurrent runs do not clash even when tests are retried.
Tip: Where possible, rely on APIs to seed or clean up data rather than using the UI for setup, which keeps tests faster and more reliable.
Warning: Sharing a single account or shopping cart across many parallel tests is a common source of non-deterministic failures.

For environments, consider whether each worker needs its own namespace (such as a tenant or database schema) or whether read-only data can be shared safely.

Common Mistakes

Mistake 1 โ€” Reusing the same mutable user for many tests

This leads to state leaks.

โŒ Wrong: Having all tests log in as qa-user@example.com and modify their profile and orders.

โœ… Correct: Use unique or partitioned users so tests cannot interfere with each other.

Mistake 2 โ€” Doing all setup through the UI in parallel

This is slow and fragile.

โŒ Wrong: Creating complex data only via web flows in every test.

โœ… Correct: Use backend APIs or fixtures to prepare data more efficiently.

🧠 Test Yourself

How can you reduce data collisions in parallel Playwright tests?