Skip to main content

Sharding and merging reports

Why This Matters

Workers run tests in parallel on one machine. When one machine is too slow, sharding splits the suite across several machines. Each machine then knows only its own part of the results, so without one extra step you get several half reports and nobody can see the whole run. This lesson does both halves: split the run, then merge the results into one report.

What you will learn

  • --shard and a matrix job in GitHub Actions
  • The blob reporter, and why each shard does not simply write HTML
  • Merging blob reports into HTML, JSON and JUnit with merge-reports

Splitting the run

--shard=1/2 means: run the first half of the tests. With fullyParallel: true, Playwright splits individual tests, not just files. You can try it locally with the unit tests:

CI=1 npx playwright test --project=unit --shard=1/2
CI=1 npx playwright test --project=unit --shard=2/2

Real output of the two commands:

  ✓  1 [unit] › tests/module-1/cart-maths.spec.ts:10:1 › subtotal adds price × quantity for every line (6ms)
  ✓  2 [unit] › tests/module-1/cart-maths.spec.ts:18:1 › total adds 21% tax, like the shop's checkout (7ms)

  2 passed (530ms)
  ✓  1 [unit] › tests/module-1/cart-maths.spec.ts:23:1 › an empty cart costs nothing (6ms)
  ✓  2 [unit] › tests/module-1/cart-maths.spec.ts:27:1 › parsePrice reads the price as the page shows it (6ms)

  2 passed (520ms)

One spec file, four tests, split two and two. CI=1 switches the config to the blob reporter, just as in CI.

In GitHub Actions, a matrix starts the same job once per value:

jobs:
  test:
    strategy:
      fail-fast: false            # let every shard finish, so the report is complete
      matrix:
        shard: [1, 2]
    steps:
      # ... checkout, npm ci ...
      - run: npm run typecheck
        if: matrix.shard == 1
      - run: npm run lint
        if: matrix.shard == 1
      - run: npx playwright test --shard=${{ matrix.shard }}/2
      - uses: actions/upload-artifact@v7
        if: ${{ !cancelled() }}
        with:
          name: blob-report-${{ matrix.shard }}
          path: blob-report/
          retention-days: 1

Some details that matter:

  • fail-fast: false. By default GitHub cancels the other shards as soon as one fails. You would lose the results of the shard that was still running, and with them half the report.
  • Typecheck and lint run once, on shard 1. Running them on every shard costs time and adds nothing.
  • if: ${{ !cancelled() }} uploads the blob report also when the tests failed. A failed run is exactly the one whose report you need.
  • retention-days: 1. Blob reports are an intermediate product, so there is no reason to keep them.

Why blob, and not HTML per shard

Each shard could write its own HTML report, but then you have two reports with half the tests each, two flaky lists, and two totals to add up. The blob reporter writes the raw events of the run (every test, every attempt, every attachment) to a zip in blob-report/. Blob reports from different shards can be combined afterwards as if they had been one run.

Merging

The report job waits for all shards (needs: test), collects the blob reports and builds the reports for people and tools:

  report:
    if: ${{ !cancelled() }}
    needs: test
    steps:
      # ... checkout, npm ci ...
      - uses: actions/download-artifact@v8
        with:
          pattern: blob-report-*
          path: all-blob-reports
          merge-multiple: true
      - run: npx playwright merge-reports --config merge.config.ts ./all-blob-reports
      - run: node scripts/summary.mjs test-results/results.json
      - uses: actions/upload-artifact@v7
        with:
          name: playwright-report
          path: |
            playwright-report/
            test-results/results.json
            test-results/junit.xml

if: ${{ !cancelled() }} matters here too. Without it, the report job is skipped as soon as one shard is red, which is exactly the run you want a report of.

merge-multiple: true puts the zips of all shards in one folder. merge-reports reads them and runs the reporters from merge.config.ts:

export default {
  testDir: "./tests",
  reporter: [
    ["html", { open: "never" }],
    ["json", { outputFile: "test-results/results.json" }],
    ["junit", { outputFile: "test-results/junit.xml" }],
  ],
};

These are the same three reporters as in a local run (lesson 37), so the files have the same names and the same shape, whether they come from your laptop or from two CI machines. Every tool built on them works in both places.

The merge also works locally, with the zips of the two local shard runs above in one folder:

$ npx playwright merge-reports --config merge.config.ts ./all-blob-reports
processing test events
building final report
finished building report

The stats in the merged test-results/results.json (shortened):

{"expected":4,"skipped":0,"unexpected":0,"flaky":0}

Four tests, one report.

Does it pay off?

These are the real numbers of run #13 on main, 127 tests:

JobDuration
test (1)1m 9s
test (2)1m 46s
report31s
whole run2m 22s

Run #13: two test jobs in a matrix, followed by the report job

Two things stand out:

  • The shards are not equally fast. Playwright splits by number of tests, in the order of the projects, not by duration. --list shows which tests each shard gets without running them:

    npx playwright test --list --shard=1/2
    
    ShardTests
    14 unit, 19 api, 1 setup, 2 with-cart, 32 chromium, 5 visual, 1 firefox (64)
    231 firefox, 32 webkit (63)

    Almost the same number of tests, but shard 2 got only browser tests, and in the two slower browsers. Shard 1 has the API and unit tests, which take milliseconds. The run is as slow as its slowest shard.

  • Every shard pays a fixed cost: starting the container, checking out, npm ci. The report job pays it once more. With a suite this size, two shards is about the limit. Each extra shard saves less test time than it costs in setup.

A rule of thumb: start sharding when one CI job takes more than about ten minutes, and add shards until the fixed cost per job is a large part of the total.

Exercise

Change the matrix to three shards in your fork: shard: [1, 2, 3], and --shard=${{ matrix.shard }}/3. Push, and compare the durations of the jobs and of the whole run with the table above. Is three better than two?

Show suggested answer

You need two changes: the list in the matrix, and the total in the --shard argument. If you change only the list, the third job runs --shard=3/2 and stops at once:

Error: --shard "3/2" current must be a positive number, not greater than shard total

Before you push, --list predicts the split:

ShardTests
14 unit, 19 api, 1 setup, 2 with-cart, 12 chromium, 5 visual
220 chromium, 22 firefox
310 firefox, 32 webkit

The slowest shard now has 42 browser tests, most of them WebKit, instead of 63. Its test time drops by about a third, which on this suite is roughly 20 to 30 seconds. Meanwhile every job still spends its fixed time starting the container and running npm ci, and the report job still comes after them. Your measured durations decide whether the gain is worth a third machine. For a suite this size, expect a small gain at best. The picture changes once a suite takes many minutes per shard.