ORDER-DEPENDENT TESTS

Green in file order, red when shuffled

L112 MINTESTING
PREDICT

Four tests share one module-level fixture and pass in file order. A teammate turns on random ordering. The code is correct. What happens?

Tests share state more often than you notice: a module-level variable, a singleton, a cache, a fixed file name, a row in a test database. If test B reads what test A wrote, B passes only after A. The runner uses file order every time, so you never see it. Then something changes the order and B fails on code that did not change.

The fix is isolation. Build a fresh fixture for each test, and clean up what the test created. Then the order cannot matter. Shuffle the order on purpose, and print the seed so a red run can be replayed. Races between tests that run in parallel are the same problem with a real data race on the shared state.

▶ SIMULATION
RUNNER
fixture: shared, module-level
ran: —
FIXTURE NOW
users: []
cache: {} · db calls: 0
WHAT THE TEAM SEES
—
last—
✓ the build is red exactly when the code is wrong (the code is right)
SCHEDULE
01[RUNNER]runner: file order, one shared fixture
02[T1]t1: register adds a user
03[T2]t2: first lookup hits the database
04[T3]t3: register is idempotent
05[T4]t4: removeAll empties the list
06[RUNNER]report
Start of file order, shared fixture scenario. 6 steps.
const fixture = { users: [], cache: new Map(), dbCalls: 0 };  // module level: every test sees it

test('register adds a user', () => {
  register(fixture, 'alice');
  expect(fixture.users).toHaveLength(1);   // true only if nothing ran before
});

Watch the invariant strip and the fixture box. In FILE ORDER each test leaves the state the next one expects, so all four are green. In SHUFFLED the same four tests run in another order and two go red. In FIX the order is the same shuffle, the fixture is rebuilt before every test, and none go red.

BREAK IT NOT YET

All four tests pass in file order (T1, T2, T3, T4) because they share one fixture. Choose a different order so that at least one test goes red. The code is correct; only the order changes.

GOAL: at least one test red

FIX IT NOT YET

Each test calls setup() and uses the fixture it returns. Rewrite setup so every test starts clean. The grader runs all 24 orders of the four tests and names the first order that fails.

Mental model:

A test is a claim about the code, and the claim must not depend on which tests ran before. Give each test its own fresh state, so any order and any number of workers gives the same answer. Shuffle on purpose, and print the seed, so a failure can be replayed.

IN PRODUCTION

Shared module-level fixtures, singletons and static fields, test databases that are never rolled back, fixed file paths and ports, caches and feature flags left switched on. It shows up when you add parallel workers, shuffle with a tool such as pytest-randomly, or run one failing test alone and it passes. A related cause of CI-only failures is timing: see flaky test from a race.

IN INTERVIEWS

“The suite passes locally and fails in CI, or passes alone and fails together. Why?” Say shared state, and name where you would look: module-level variables, caches, files, ports and database rows. Describe the fix as a fresh fixture per test plus randomised order with a printed seed, and say how you would find the culprit: bisect the failing order down to the pair of tests that interact.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: The mock that lied →