QA AND TEST AUTOMATION · PART 8 · CI/CD AND TEST INFRASTRUCTURE · SCALE · 77 OF 112

Sharding and Parallel Test Runs in CI

Making big suites finish in minutes: workers on one machine versus shards across machines, Playwright --shard with blob reports merged into one report, pytest-xdist and JUnit 5 parallel execution, and the real cost of shared state. Run for real on ShopRung: pytest -n 4 failed 2 to 5 of 23 tests because of a shared reset, and one ShopRung per worker fixed it.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Workers vs shards
  • --shard + blob + merge-reports
  • shared reset broke -n 4
  • one app per worker fixed it
  • measure

Notes

Two levels

There are two levels. Workers are parallel processes on one machine, limited by its CPU and memory; Playwright workers, pytest dash n, JUnit five parallel execution and TestNG all do this. Shards split the suite across several machines, usually several CI jobs, each running one part. You can combine them: four shards with two workers each run eight tests at a time.

Workers vs shards
  • workers: parallel processes on ONE machine (CPU, memory) Playwright workers, pytest -n, JUnit 5 parallel, TestNG
  • shards: the suite split across SEVERAL machines (CI jobs) --shard=1/4 ... 4/4
  • both: 4 shards x 2 workers = 8 tests at a time

Playwright shards

In Playwright, the shard option takes a part and a total, like one of four. In CI, a matrix starts four jobs, one per shard. Each job uses the blob reporter, which writes a compact result file, and uploads it as an artifact with a unique name. Without fully parallel mode, Playwright splits by file, so balance how many tests each file has.

GitHub Actions matrix
strategy:
  fail-fast: false
  matrix:
    shard: [1/4, 2/4, 3/4, 4/4]
steps:
  - run: npx playwright test
      --shard=${{ matrix.shard }}
      --reporter=blob
  - uses: actions/upload-artifact@v4
    with: { name: blob-${{ strategy.
      job-index }}, path: blob-report }

Merge reports

We ran the ShopRung Playwright suite from the labs in two shards. Each shard ran the login setup project plus three tests: setup is a dependency, so every shard runs it. A final CI job downloads all blob files into one folder and runs merge reports with the HTML reporter. The result is one report with all eight results, as if the suite had run on one machine.

ShopRung UI suite, 2 shards
$ npx playwright test --shard=1/2
    --reporter=blob
  4 passed   (setup + 3 tests)
$ npx playwright test --shard=2/2
    --reporter=blob
  4 passed   (setup + 3 tests)
$ npx playwright merge-reports
    --reporter html ./all-blob
  → one report: 8 results

Shared state

Now the catch. Our pytest API suite passes all twenty three tests in a row. With pytest x dist and four workers, three runs failed two, four and five tests. Grouping by file with dist load file still failed one or two. The reason: every test resets ShopRung's whole database before it starts, and the cart and order tests share Alice. One worker's reset wipes another worker's cart mid-test.

ShopRung API suite, 23 tests
pytest                  23 passed
pytest -n 4  (3 runs)    2, 4, 5
                         failed
pytest -n 4 --dist loadfile
             (5 runs)    1-2 failed

why: every test calls
  POST /api/test/reset on the ONE
  shared ShopRung

Isolate the data

The fix is isolation. We started four ShopRungs on ports eighty two hundred to eighty two oh three, each with its own database file, and the suite picks its server from the worker id that x dist sets. Five runs out of five passed all twenty three tests. Other ways to isolate: a unique user per worker, data each test creates and owns, or a separate database schema per worker.

conftest.py
# xdist sets gw0, gw1, ... per worker
WORKER = os.getenv(
    "PYTEST_XDIST_WORKER")
if WORKER:
    BASE_URL = "http://localhost:"
      + str(8200 + int(WORKER[2:]))

# 4 ShopRungs on 8200-8203, each
# with its own SHOPRUNG_DB file
→ pytest -n 4: 23 passed, 5 of 5

Java side

In Java, JUnit five turns on parallel execution with properties in junit platform dot properties: enabled true, mode concurrent, and a dynamic strategy based on CPU cores. Tests that touch a shared resource get a resource lock annotation, so JUnit runs them one at a time. TestNG uses the parallel and thread count attributes in its XML file. Thread safety matters: one WebDriver per thread, never a shared static one.

junit-platform.properties
junit.jupiter.execution.parallel
  .enabled = true
junit.jupiter.execution.parallel
  .mode.default = concurrent
junit.jupiter.execution.parallel
  .config.strategy = dynamic

@ResourceLock("shoprung-db")
  // run these one at a time
TestNG: parallel="methods"
  thread-count="4"

Is it faster?

Is it faster? Measure it. Our twenty three API tests took one point eight seconds in one process and about two point one with four workers: starting workers cost more than it saved. Parallel runs pay off for slow tests, like UI tests and long API flows. Balance shards by duration, not test count, and watch the slowest shard, because the job finishes only when it does.

Measured here
  • API suite, 23 fast tests: 1 process 1.8 s -n 4 2.1 s → start-up costs more than it saves
  • parallel pays off for SLOW tests: UI, long API flows
  • balance shards by duration, not by count

Common mistakes

Parallel mistakes. A global reset while other workers are running. One shared user and cart for every test. A static WebDriver shared across threads. Tests that only pass in a certain order. Shards whose reports are never merged, so nobody sees the whole result. And more workers than the machine has CPUs, which makes everything slower and flakier.

Avoid these
  • a global reset with parallel workers
  • one shared user and cart
  • static WebDriver across threads
  • tests that depend on order
  • no merged report (4 reports nobody reads)
  • more workers than CPUs

Say it like this

In the interview: first push checks down the pyramid. Then run in parallel: workers on each machine, and a CI matrix of shards whose blob reports are merged into one. Parallel runs need independent tests, so each worker or test owns its data, there are no global resets and no shared drivers. Balance shards by duration and measure the gain, because parallel has a start up cost.

In the interview
  • "Push checks down the pyramid
  • first. Then parallel: workers per machine and a CI matrix of shards, blob reports merged into one. Tests must be independent: own data per worker or test, no global resets, no shared
  • drivers. Balance shards by duration and measure, because
  • parallel has a start-up cost."

Interview questions

Q1What is the difference between workers and shards?

Workers are parallel processes on one machine; shards split the suite across several machines or CI jobs, each running one part.

Q2How do you shard Playwright tests in CI?

A matrix of jobs running npx playwright test --shard=i/n with the blob reporter, then a job that runs merge-reports on all blob files.

Q3Why did pytest -n 4 fail on the ShopRung API suite?

Every test reset the single shared database, and tests shared one user, so workers wiped each other's data mid-test.

Q4How do you isolate data for parallel workers?

A separate app or database per worker, unique users per worker, or data each test creates and owns; never a global reset.

Q5How do you enable parallel tests in JUnit 5?

Set junit.jupiter.execution.parallel.enabled=true and mode.default=concurrent, and use @ResourceLock for shared resources.

Q6What does pytest-xdist --dist loadfile do?

Sends all tests from one file to the same worker; it helps when state is shared within a file, not across files.

Q7Is running in parallel always faster?

No. Workers have start-up cost; for small, fast suites it can be slower. It pays off for slow tests. Measure.

Q8How should shards be balanced?

By test duration rather than count, since the job finishes only when the slowest shard does.