πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Questions
Question 78 of 100
Execution
Advanced

What are the trade-offs of parallel execution?

The Answer

Parallel execution is faster but requires tests to be fully independent (no shared state). It uses more resources (CPU/RAM) and can cause flakiness if tests compete for the same test data.

Deep Dive Explanation

The biggest challenge with parallel tests is database contention β€” if two parallel tests try to create/modify the same database record, one will fail. Strategies: 1) Use unique identifiers per test, 2) Use separate test databases per worker, 3) Mock the database/API layer entirely.

example.spec.ts
// Trade-offs table (conceptual):
// βœ… PROS:               ❌ CONS:
// Faster execution       More CPU/RAM usage
// Scales with hardware   Tests must be stateless
// Finds ordering bugs    DB conflicts if tests share data
// CI time reduction      Harder to debug parallel failures

// SOLUTION: Use unique test data per worker
test('create unique user', async ({ page }, testInfo) => {
  const uniqueEmail = `user-${testInfo.workerIndex}-${Date.now()}@test.com`;
  await page.getByLabel('Email').fill(uniqueEmail);
  // Each parallel worker uses a different email - no conflicts
});