Parallel Test Execution with Sharding
Advanced15 min
Scale Playwright test suites by splitting tests across multiple CI machines using sharding for dramatically faster feedback loops.
Prerequisites
- -Playwright test suite with many tests
- -GitHub Actions or similar CI with matrix support
Steps
1
Run tests with sharding locally
Split test files across multiple shards for parallel execution.
$ npx playwright test --shard=1/3
Sharding splits test files, not individual tests. Use --shard=N/M where N is the shard index (1-based) and M is the total number of shards.
2
Configure CI matrix for sharding
Set up GitHub Actions to run shards in parallel across machines.
$ cat > .github/workflows/playwright-sharded.yml << 'EOF'
name: Playwright Sharded
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1/4, 2/4, 3/4, 4/4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npx playwright install --with-deps chromium
- run: npx playwright test --shard=${{ matrix.shard }} --project=chromium
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: blob-report-${{ strategy.job-index }}
path: blob-report/
merge-reports:
if: ${{ !cancelled() }}
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- uses: actions/download-artifact@v4
with:
path: all-blob-reports
pattern: blob-report-*
merge-multiple: true
- run: npx playwright merge-reports --reporter html ./all-blob-reports
- uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report/
EOF
3
Configure blob reporter for shard merging
Use the blob reporter to collect results from each shard for merging.
$ cat << 'EOF'
// playwright.config.ts
reporter: process.env.CI
? [['blob'], ['github']]
: [['html']],
EOF
4
Configure parallel workers per shard
Tune the number of parallel workers within each shard.
$ cat << 'EOF'
// playwright.config.ts
fullyParallel: true,
workers: process.env.CI ? 2 : undefined,
EOF
Each shard runs its own set of workers. With 4 shards and 2 workers each, you get 8 parallel test executions across your CI cluster.
Full Script
FAQ
Discussion
Loading comments...