Introduction
Playwright Test runs tests in parallel by default using multiple worker processes. This significantly reduces test execution time.How Parallelization Works
Fromsrc/common/config.ts:121,240-252 and src/runner/testRunner.ts:88-115:
Playwright uses a worker-based parallelization model:
- Test runner spawns multiple worker processes
- Each worker runs tests sequentially
- Workers run in parallel to each other
- Tests are distributed across available workers
Worker Configuration
Fixed Number of Workers
Percentage of CPU Cores
Fromsrc/common/config.ts:240-252:
Automatic Detection
Environment-Based Configuration
Parallel Modes
Fromsrc/common/test.ts:58 and src/common/testType.ts:46-51,155-168:
Default Mode
Tests within a file run sequentially:Fully Parallel Mode
Run all tests in parallel: Fromsrc/common/config.ts:102:
Describe Parallel
Make specific describe blocks run in parallel: Fromsrc/common/testType.ts:47,155-158:
Serial Mode
Force tests to run serially: Fromsrc/common/testType.ts:49,155-156:
Worker Isolation
Each worker maintains its own isolated environment:Worker-Scoped Fixtures
Shared within a worker, isolated between workers:Browser Instance Sharing
Fromsrc/index.ts:104-126:
Browser instances are shared within workers:
- Launches one browser instance
- Creates fresh contexts for each test
- Reuses browser across tests
Controlling Parallelization
Project-Level Workers
Fromsrc/common/config.ts:212-215:
Configure Mode
Fromsrc/common/testType.ts:186-209:
Worker Index
Access worker index in tests:Sharding
Distribute tests across multiple machines: Fromsrc/common/config.ts:116:
Configuration
CLI Usage
CI/CD Example
Test Distribution
Tests are distributed to balance execution time:Parallel Execution Patterns
Independent Tests
Tests that don’t share state:Shared Setup with Parallel Tests
Sequential Test Flow
Performance Optimization
Optimal Worker Count
Test Organization
Browser Reuse
Worker-scoped browser fixture reuses browsers:Debugging Parallel Tests
Run Tests Serially
View Worker Output
Test Info Debugging
Common Pitfalls
Shared Mutable State
File System Operations
Database Conflicts
Best Practices
- Design for parallelization: Make tests independent
- Use appropriate worker count: Balance speed and resources
- Avoid shared state: Each test should be self-contained
- Use worker-scoped fixtures: For expensive setup
- Leverage sharding: For very large test suites
- Monitor CI resources: Adjust workers based on available CPU
- Use serial mode sparingly: Only when necessary
Example: Optimal Configuration
Next Steps
Retries
Learn about test retries
Configuration
Configure worker settings
