ORDER-DEPENDENT TESTS
Green in file order, red when shuffled
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.
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
});let fixture;
beforeEach(() => { fixture = { users: [], cache: new Map(), dbCalls: 0 }; }); // new state for every test
test('register adds a user', () => {
register(fixture, 'alice');
expect(fixture.users).toHaveLength(1); // true in any order
}); 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- A first unit testSoftware Testing and Quality
- Integration tests with a real databaseSoftware Testing and Quality
- Test data and fixtures: building the world a test needsSoftware Testing and Quality