Playwright Test Sharding

A teacher with four hundred exam papers asks three colleagues to share the marking. Each person takes a quarter of the stack, and all four finish in a quarter of the time. Sharding splits a test suite across several machines in the same way, and the full run finishes far sooner.

Workers and Shards

Workers and shards both divide work, and they act at different levels.

  • A worker is a process on one machine that uses one processor core.
  • A shard is a slice of the suite that runs on a separate machine.

Large suites combine both. Each machine runs one shard, and each shard uses several workers.

The Sharding Diagram

                 400 tests
                     |
     +---------+-----+-----+---------+
     |         |           |         |
  Shard 1/4  Shard 2/4  Shard 3/4  Shard 4/4
  100 tests  100 tests  100 tests  100 tests
  Machine A  Machine B  Machine C  Machine D
     |         |           |         |
     +---------+-----+-----+---------+
                     |
               Merged report

The Shard Command

The --shard option takes the form current/total.

npx playwright test --shard=1/4
npx playwright test --shard=2/4
npx playwright test --shard=3/4
npx playwright test --shard=4/4

Each command runs on its own machine. Playwright calculates the slice from the test list, so every test lands in exactly one shard.

Balancing the Load

Playwright splits work more evenly when tests run in parallel inside files. The config setting below lets individual tests spread across shards rather than whole files.

export default defineConfig({
  fullyParallel: true,
});

The Report Problem

Each shard produces its own report, which leaves four partial reports. The blob reporter saves raw results, and the merge-reports command combines them into one HTML report.

export default defineConfig({
  reporter: process.env.CI ? 'blob' : 'html',
});
npx playwright merge-reports --reporter html ./all-blob-reports

Choosing the Number of Shards

More shards cut the run time, and each shard adds startup cost for installing dependencies and browsers. A suite that finishes in six minutes gains little from eight shards. A suite that takes an hour benefits greatly from six or eight.

Shards   Estimated time for a 60 minute suite
  1      60 minutes
  2      30 minutes + setup
  4      15 minutes + setup
  8       8 minutes + setup

A Real-World Scenario

A software company runs 1,200 browser tests before every release. The full run once took fifty minutes on a single build machine. The team split the suite into five shards on five CI machines. The run now finishes in eleven minutes, and engineers receive results before their coffee cools.

Good Habits

  • Keep tests independent so any test can land in any shard.
  • Avoid shared data that one test writes and another reads.
  • Merge blob reports in a final job to see one combined result.
  • Watch the slowest shard, because it decides the total time.

Quick Practice Task

Take a project with at least eight tests. Run it with --shard=1/2 and again with --shard=2/2. Add the test counts from both runs and confirm the sum equals the full suite.

Common Questions

Do shards share state?

They run on separate machines and share nothing. Each shard needs its own setup, data, and login session.

What if one shard runs much longer?

Uneven shards usually come from a few very slow files. Enable fullyParallel or split large files into smaller ones.

Can sharding run on one machine?

It can, though workers fit that case better. Sharding pays off when several machines run at the same time.

Key Takeaways

  • Sharding divides a suite across machines, and workers divide it across cores.
  • The --shard=current/total option selects a slice.
  • The blob reporter and merge-reports command build one final report.
  • Independent tests make sharding safe.