Yeda AI Tips · #076

Español

Shuffle Your Test Suite to Expose Order-Dependent Tests

Shuffle your tests, then watch them break. If a suite only passes in one specific order, it isn't testing your code — it's testing an accident. Hidden shared state between tests is a top cause of flaky builds, and it stays invisible until something changes the run order: a new file, a parallel worker, a CI machine. Randomizing the order on purpose surfaces those failures on your machine, today, instead of as a random red build next month.

Why order dependence hides so well

In a fixed order, test B always runs after test A — so B silently inherits whatever A left behind: a cached singleton, a mutated module-level object, a row still sitting in the test database, an environment variable nobody unset. Every local run replays the same order, so every local run is green. Then CI splits the suite across workers, or someone adds a file that sorts earlier, and the dependency snaps. The failure looks random; the cause never was.

The fix for detection is one flag away: make random order the default, so any new coupling breaks immediately, while the guilty commit is still on top.

The shuffle flag in your runner

RunnerShuffleReproduce a failing order
Vitest--sequence.shuffle.tests (files: --sequence.shuffle.files)--sequence.seed <n>
Jest--randomize (jest-circus runner; prints the seed)--seed=<n>
pytestinstall pytest-randomly — shuffles automatically--randomly-seed=<n> or --randomly-seed=last
Gogo test -shuffle=on (seed from clock, reported in output)go test -shuffle=<N>
RSpec--order rand--seed 123 (same as --order rand:123)

Every one of these prints or accepts a seed. That's the whole workflow: shuffle finds the failure, the seed replays it deterministically while you debug.

# Vitest: shuffle tests, then replay the exact failing order
vitest run --sequence.shuffle.tests
vitest run --sequence.shuffle.tests --sequence.seed 1734

# pytest with pytest-randomly: rerun the last recorded seed
pytest --randomly-seed=last

Fix the state, not the order

When a shuffled run fails, resist the urge to pin the order back. The failure is telling you two tests share state. Typical fixes:

Power-user notes

Make order independence a rule, not a hope — a suite that passes in any order is a suite you can parallelize, split, and trust.

Resources

Building an AI-assisted engineering workflow? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · More tips · Leer en español