Parallelism and Sharding
Relevant source files
The following files were used as context for generating this wiki page:
Playwright Test is designed for efficient test execution, leveraging parallelism and sharding to significantly reduce the time required to run a full test suite. Parallelism enables the concurrent execution of tests on a single machine by utilizing multiple worker processes. Sharding, on the other hand, allows for the distribution of the test suite across multiple machines, a common practice in CI/CD environments to achieve massive parallelization.
These features are primarily controlled through the playwright.config.ts file, offering granular control over how tests are executed. Understanding and correctly configuring these options is crucial for optimizing test runs, especially in large-scale projects.
Configuration
Parallelism and sharding are configured in your playwright.config.ts file. The main options are workers, fullyParallel, and shard. You can also control execution mode at a more granular level within your test files.
Sources: packages/playwright/types/test.d.ts
Key Configuration Options
| Option | Type | Description | Source |
|---|---|---|---|
workers | number | string | The maximum number of concurrent worker processes. Can be a number (e.g., 4) or a percentage of logical CPU cores (e.g., '50%'). Defaults to half the number of logical CPU cores. This can also be set on a per-project basis. | packages/playwright/types/test.d.ts:1330-1346 |
fullyParallel | boolean | If true, tests in all files are run in parallel. By default, tests within a single file are run serially. | packages/playwright/types/test.d.ts:983-989 |
shard | { current: number, total: number } | Configures the test run to execute only a specific fraction of the test suite. total is the number of shards, and current is the one-based index of the shard to run. | packages/playwright/types/test.d.ts:1274-1288 |
test.describe.configure({ mode }) | 'default' | 'parallel' | 'serial' | Sets the execution mode for a specific describe block, overriding the global or project-level settings. | packages/playwright/types/test.d.ts:2238-2281 |
Sharding Configuration
Sharding is enabled by providing the shard option in the configuration or via the command line. For example, to split the test suite into three shards and run the second one:
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
shard: { total: 3, current: 2 },
});
This configuration is ideal for CI environments where you can spin up multiple machines, each running a different shard of the test suite.
Sources: packages/playwright/types/test.d.ts:1274-1288, tests/playwright-test/shard.spec.ts:152-157
Execution Model
Playwright's test runner orchestrates test execution by managing a pool of worker processes and distributing tests to them based on the configuration.
Parallelism and Workers
When tests are run in parallel, Playwright spawns multiple worker processes. Each test has access to TestInfo, which provides details about the execution context, including two important properties related to parallelism: workerIndex and parallelIndex.
| Property | Description | Source |
|---|---|---|
testInfo.workerIndex | A unique, auto-incrementing index for each worker process created. A new worker process (with a new workerIndex) is created if a previous one fails, ensuring test isolation. | tests/playwright-test/worker-index.spec.ts:140-153 |
testInfo.parallelIndex | The index of the parallel execution slot, from 0 to workers - 1. This index is stable and identifies the "lane" of execution, even if the underlying worker process is restarted. | tests/playwright-test/reporter-json.spec.ts:403-421, tests/playwright-test/worker-index.spec.ts:14-41 |
The following diagram illustrates the lifecycle of workers and the distinction between workerIndex and parallelIndex.
This sequence shows that parallelIndex remains consistent for a parallel "slot", while workerIndex is unique to each new process.
Sources: tests/playwright-test/worker-index.spec.ts:140-153
Worker Lifecycle
- Reuse: A worker process is reused for subsequent tests within the same test file to improve performance, as long as no tests fail.
Sources: tests/playwright-test/worker-index.spec.ts:63-75 - Restart on Failure: If a test fails, the worker process that ran it is terminated, and a new one is spawned for subsequent tests. This ensures that a failing test cannot affect others.
Sources: tests/playwright-test/worker-index.spec.ts:140-153 - Execution Modes:
test.describe.configure({ mode: 'parallel' })allows tests within a single file to run in parallel across different workers.test.describe.configure({ mode: 'serial' })ortest.describe.serial()forces tests within a group to run sequentially in the same worker. If one test fails, the rest in the group are skipped.- The default mode is to run tests within a file sequentially in the same worker.
Sharding Logic
Sharding distributes test files across the total number of shards. The test runner on each shard is responsible for running only its assigned portion of the tests.
The diagram below shows how a test suite is divided among three shards.
This diagram illustrates the distribution of test files into shards for parallel execution on different CI machines and the subsequent merging of their reports.
Sources: tests/playwright-test/shard.spec.ts, tests/playwright-test/reporter-blob.spec.ts
Sharding Behavior
- Test Filtering: When sharding is active, tests that do not fall into the
currentshard are not executed and are not reported as "skipped". This prevents inflation of skipped test counts in reports.
Sources: tests/playwright-test/reporter-json.spec.ts:47-68 - Dependencies: Project dependencies (e.g.,
setupprojects) are also subject to sharding. If a test project depends on asetupproject, and thatsetupproject is allocated to a different shard, the dependency will be skipped for the current shard's run.
Sources: tests/playwright-test/shard.spec.ts:178-201 mode: 'default'Suites: Test suites that are configured withmode: 'default'(which is the default behavior for tests in a file) are not split across shards. If any test from such a suite is assigned to a shard, the entire suite (including itsbeforeAllhooks) will run on that shard.
Sources: tests/playwright-test/shard.spec.ts:233-277beforeAllHooks: For suites running in parallel mode,beforeAllhooks are sharded along with the tests. This means abeforeAllhook might run on multiple shards if the tests from its suite are distributed across them.
Sources: tests/playwright-test/shard.spec.ts:281-314
Summary
Parallelism and sharding are powerful features in Playwright Test that enable fast and efficient execution of test suites. By using multiple worker processes, tests can be run concurrently on a single machine. For even greater speed, sharding distributes the test suite across multiple machines. Proper configuration of workers, fullyParallel, and shard options, along with an understanding of the execution model, allows development teams to scale their testing efforts effectively and maintain short feedback cycles in their CI/CD pipelines.