QA AND TEST AUTOMATION · PART 3 · AUTOMATION FUNDAMENTALS · RELIABILITY · 36 OF 112

Test Reliability at Scale: Shuffle, Isolate, Retry

Keeping a suite of thousands of tests fast and trustworthy: isolation by default, parallel workers and sharding, shuffled order to expose hidden dependencies, locks for the few shared resources, isolated retries, flake dashboards and quarantine workflows, and why flaky tests cluster around a shared cause. With the 2026 options in Playwright, JUnit and pytest.

  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 / 12

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

Key takeaways

  • Tiny flake rates multiply
  • isolate
  • parallel and shard
  • shuffle
  • lock the few
  • isolated retries
  • fix shared causes

Notes

The maths

Here is why scale changes things. One test that flakes one time in a thousand is barely noticeable. Three thousand tests, each flaking one time in a thousand, means the chance that a run is fully green is zero point nine nine nine to the power of three thousand, about five percent. Ninety five percent of runs would show a false red. Reliability is a property of the whole suite, so tiny per test flake rates matter enormously.

Why scale changes things
1 test, 0.1% flaky:
  fails 1 run in 1,000
3,000 tests, each 0.1% flaky:
  P(all pass) = 0.999^3000
              ≈ 5%
→ 95% of runs have a false red

reliability is a property of
the whole suite, not one test

Isolation

The foundation is isolation. Each test gets a fresh world. Playwright gives each test a new browser context by default, with its own cookies, storage and cache, like a brand new incognito window. Each test creates its own user and data, uses no shared static state, and never relies on the order tests run in. It costs a little setup time, and buys the ability to run tests in any order, on any machine, in parallel.

Isolation by default
  • Playwright: a new browser context per test: fresh cookies, storage, cache
  • own data: unique user, cart
  • no shared static state
  • no reliance on test order
  • cost: a little setup time
  • payoff: any order, any machine, in parallel

Parallel and shards

Isolated tests can run in parallel. Workers run tests side by side on one machine: in Playwright, set fully parallel and a number of workers; in pytest, the xdist plugin's dash n auto; in JUnit five, switch on parallel execution. Sharding splits the suite across machines: Playwright's shard two of five runs the second fifth. A forty minute suite on five shards takes about eight. Part eight sets this up in CI.

Run it faster
# Playwright
fullyParallel: true, workers: 4
npx playwright test --shard=2/5
# pytest
pytest -n auto   (pytest-xdist)
# JUnit 5
junit.jupiter.execution
  .parallel.enabled=true

40 min / 5 shards ≈ 8 min

Shuffle

Tests that pass in one order can hide dependencies: test B quietly relies on data test A created. Shuffling the order exposes them. Recent Playwright versions have a shuffle option, and you can pass a seed to replay the same order. pytest has random order plugins. When a test fails only in a certain order, you have found a hidden dependency. Keep the seed from the failing run so you can reproduce it exactly.

Shuffle the order
# Playwright (2026)
npx playwright test --shuffle
  --shuffle 1234   # replay
# pytest
pytest --random-order
  (pytest-random-order)

a failure only in one order
= hidden dependency
keep the seed to reproduce

Locks

Sometimes a resource really is shared. ShopRung's bug switch is global: if two tests flip it at the same time, they break each other. Rather than running the whole suite one test at a time, lock just those tests. Recent Playwright versions let a group of tests declare a named lock, so tests with the same lock never overlap while everything else stays parallel. Other tools use serial groups or resource tags. Lock the few, run the rest in parallel.

Locks for shared things
// tests touching one shared
// resource must not overlap
test.describe.configure({
  lock: "bug-switch" });

ShopRung: the bug switch is
GLOBAL; two tests flipping it
at once break each other

lock the few, parallel the rest

Isolated retries

If you retry, retry honestly. A retry in the same worker may pass or fail because of state left behind by the first attempt. Retrying in a fresh, isolated worker gives a cleaner answer, and recent Playwright versions offer that as a retry strategy. Either way, the report should say flaky, not passed, for a test that needed a retry, and each flaky result should become a ticket. Retries keep the pipeline moving; they do not make the problem go away.

Retries that tell the truth
  • retry in a FRESH worker, so leftover state from the failure does not decide the retry (Playwright 2026: retryStrategy: "isolated")
  • report: passed / failed / FLAKY
  • flaky is a bug with a ticket, not a pass

Flakes cluster

Flaky tests rarely fail alone. A 2025 study of twenty four Java projects found that three quarters of eight hundred and ten flaky tests failed in clusters that shared a cause. One slow service, one shared fixture, or one too short timeout can make thirty tests flake together. So group failures by their error and their timing. Fixing one root cause can clear dozens of flaky tests at once, which is far cheaper than fixing them one by one.

Systemic flakiness
  • research on 24 Java projects (2025): 75% of 810 flaky tests failed in clusters with a shared cause
  • one slow service, one shared
  • fixture, one timeout value → 30 tests flake together
  • group failures by cause

Treat flakiness as data

At scale, run reliability like a product. A dashboard shows the flake rate per test, per suite and per team over time. Tests above a threshold are quarantined automatically, with a ticket. Every test has an owner. Set budgets, for example a suite that is fully green on at least ninety eight percent of clean runs, and finishes in under fifteen minutes per pull request. Review the ten worst tests every week. What gets measured gets fixed.

Run it like a product
  • dashboard: flake rate per test, suite, team, over time
  • auto-quarantine above a threshold, with a ticket
  • every test has an owner
  • budget: e.g. suite green rate above 98%
  • suite time budget: e.g. under 15 minutes per PR
  • review the worst 10 weekly

Common mistakes

Scale mistakes. Switching on parallel runs before tests are isolated, which turns hidden dependencies into random failures. One shared test account for everything. Global switches, like feature flags or ShopRung's bug switch, without locks. Retries that hide flakes in a green report. Tests with no owner. Shards split by file count instead of run time, so one shard takes three times longer than the others. And never shuffling, so hidden dependencies stay hidden.

Avoid these
  • running in parallel before tests are isolated
  • one shared test account
  • global switches without locks
  • retries that hide flakes
  • no owner per test
  • sharding by file count, not by run time
  • never shuffling the order

Say it like this

In a senior interview: isolation first, with a fresh browser context and its own data for every test. Then parallel workers and shards for speed, shuffled order to find hidden dependencies, locks only for truly shared resources, and retries in a fresh worker that are reported as flaky, not passed. On top, a flake dashboard with owners and budgets, and since flakes cluster, I fix shared causes first.

In the interview
  • "Isolation first: fresh
  • context and own data per test. Then parallel workers
  • and shards, shuffled order to find hidden dependencies, locks only for truly shared
  • resources, retries in a fresh
  • worker reported as flaky, and a flake dashboard with owners, fixing shared causes first."

Interview questions

Q1Why does flakiness get worse as a suite grows?

Per-test flake rates multiply: with 3,000 tests each 0.1% flaky, only about 5% of runs are fully green, so false failures become routine.

Q2What is test isolation and why does it matter at scale?

Each test runs with its own fresh state (browser context, data, user), so tests can run in any order and in parallel without affecting each other.

Q3What is the difference between parallel workers and sharding?

Workers run tests concurrently on one machine; sharding splits the suite across several machines or CI jobs, each running a part.

Q4Why run tests in random order?

To expose hidden dependencies between tests; a test that fails only in some orders relies on state from another test. Keep the seed to reproduce.

Q5When would you use a lock or serial group in a parallel suite?

For the few tests that touch a truly shared resource, such as a global feature flag or a single external sandbox, so they never overlap while others stay parallel.

Q6What is an isolated retry?

Re-running a failed test in a fresh worker so leftover state from the first attempt cannot affect the result; the outcome should be reported as flaky.

Q7What does "flaky tests cluster" mean in practice?

Many flaky tests often share one root cause (a slow dependency, shared fixture or timeout), so grouping failures by cause and fixing it clears many tests at once.

Q8What metrics would you track for suite reliability?

Flake rate per test and suite, green-run rate, quarantine count and age, suite duration per pull request, and time lost to re-runs.