Skip to main content

Test data that does not leak

Why This Matters

Tests that share data pass alone and fail together. Test A adds a product to the cart; test B, running at the same time, expects an empty cart. Run them one by one and both pass; run the suite with four workers and B fails now and then. That is not a flaky test; it is two tests fighting over the same data. Every test should create what it needs and see nothing of what other tests do.

The rule

A test may only depend on:

  1. Data it creates itself, in its own fixture or body.
  2. Read-only data that nobody changes, such as the product catalogue.

Everything else (a shared cart, a shared user, “the order from the previous test”) is a leak waiting to happen.

Sandboxes: isolation built into the site

practiceautomatedtesting.com makes rule 1 easy: every client gets its own sandbox with its own cart, orders and stock.

  • In the browser every new context gets a new shop_session cookie, so every Playwright test starts with an empty cart. You have seen it in every run so far: dozens of browser tests side by side, and none of them ever found another test’s products in its cart.
  • Through the API the api fixture sends a fresh X-Session-Id per test:
sandboxId: async ({}, use, testInfo) => {
  await use(`pw-w${testInfo.workerIndex}-${randomUUID()}`);
},

The UUID makes the sandbox unique. The worker index (pw-w3-…) makes it traceable: in the server’s logs you can see which worker made a request.

The tests that change stock (orders, cancelling) work only because of this. Two tests ordering Bluetooth Headphones at the same time each see their own stock go from 20 to 18.

Start clean, explicitly

A new sandbox is empty, but a test that must be sure resets it anyway:

test.beforeEach(async ({ api }) => {
  // Start clean, even if this sandbox id was ever used before.
  expect((await api.post("reset")).ok()).toBeTruthy();
});

It costs one fast request, and it documents what the test assumes.

Where shared data is fine

The catalog worker fixture (lesson 32) is shared by every test in a worker, and that is safe: nobody changes it. The saved session from lesson 19 is the counter-example: it is one sandbox shared by every test that starts from it, so it is only safe for tests that read the cart, never for tests that change it.

On an application without sandboxes

Most real applications have no sandboxes. The same rule then needs more work:

  • Unique data per test: a user called test-${randomUUID()}@example.com, an order reference with the test’s id in it.
  • Cleanup in fixtures, after use(), so it also runs when the test fails.
  • Never depend on order: no test may need another test to have run first. fullyParallel: true in the config shuffles them across workers, which finds such dependencies quickly.

Check it: run in parallel, many times

npx playwright test --workers=4 --repeat-each=3

A suite that leaks data fails here, at random. A suite with isolated data passes every time, whatever the order.

Exercise

Write a test that checks a new sandbox has no orders (GET orders returns an empty list), and another that places an order and checks there is exactly one. Run both with --repeat-each=10 --workers=4. Then change the sandboxId fixture to return the same id for every test, and run again. What happens, and why?

Show suggested answer

With a sandbox per test, both tests pass every time: each sees only its own orders.

With one shared id, the results depend on timing. The “no orders” test fails whenever an order test ran before it in that sandbox, and “exactly one order” fails when two order tests overlapped, or the reset in another test wiped the order mid-test. The tests are correct; the data is shared. That is the difference between a flaky test and a leaking test: the fix is not in the test, but in where its data comes from.