Why 37 Tests Failed After Runner Order Change
In this article
When a test runner's execution order changes, a sudden batch of failures—like 37 tests—almost always points to hidden test interdependence, not a problem with the tests themselves. Tests that pass in one order and fail in another are sharing state, relying on a global variable, touching the same file, or depending on a side effect from a previously run test. The fix is to make each test fully independent, and the first step is reproducing the failure with a specific order.
Background: How Test Runners Order Tests
Most test frameworks (JUnit, pytest, Mocha, RSpec, etc.) do not guarantee a stable execution order by default. They may run tests in the order they were defined, alphabetically, by file path, or in a randomized order. Some runners even support a --shuffle or --order random flag to help surface dependencies. When a developer changes the runner's configuration—say, from alphabetical to random—the order changes, and any test that secretly depends on another test's side effects will break.
The Core Problem: Test Interdependence
Tests should be isolated: each one should set up its own data, run its own assertions, and clean up after itself. When they aren't, they pass or fail based on what ran before. Common forms of interdependence include:
- Shared global state: A module-level variable, a singleton, or a static property that one test modifies and another reads.
- Filesystem or database: Tests that write to the same temp file, database table, or cache without clearing it first.
- Environment variables: A test that sets
process.envoros.environand doesn't restore it. - Network or time: Tests that mock a network call or freeze the clock, and forget to unmock or unfreeze.
- Order of setup/teardown: A fixture or hook that runs only once (like
beforeAll) and is assumed to have run before every test.
When the order changes, a test that previously ran after the "setup" test now runs before it, and fails because the expected state isn't there.
How to Diagnose the 37 Failures
If you suddenly have 37 failures after an order change, don't panic. Follow these steps to isolate the cause:
- Re-run with the new order a few times. If the failures are flaky, run the suite multiple times to see if the same tests fail each time.
- Run the failing tests alone. If they pass in isolation, they depend on something from another test.
- Run the suite in reverse order. If a different set fails, that confirms order-dependence.
- Add logging or use a debugger to see what state is present when a test starts.
- Check for shared resources—files, databases, ports, or environment variables—that tests might be leaving behind.
For example, if test A creates a user in a database and test B assumes that user exists, then running B before A will fail. The fix is to have B create its own user in a setUp method.
Common Mistakes That Lead to Order-Dependent Tests
Developers often introduce these issues without realizing it:
- Using a shared fixture that is mutated. A test might change a list or object that another test reads.
- Not cleaning up after tests. Files, temp directories, or database rows left behind can affect later tests.
- Relying on test execution speed. If a test waits for an async operation and another test clears the state too soon, timing can cause failures.
- Mocking without restoring. If a test mocks a function and doesn't restore it, later tests may call the mock instead of the real function.
- Assuming a specific order of test files. Some runners execute files in a deterministic order, but changing the runner's config can alter that.
A classic example: Test 1 sets localStorage.setItem('token', 'abc'). Test 2 reads localStorage.getItem('token') and expects it to be 'abc'. If the runner runs Test 2 first, it gets null and fails. The fix is for Test 2 to set its own token in its setup.
How to Fix Order-Dependent Tests
The goal is to make every test independent, so order doesn't matter. Here are concrete strategies:
- Use setUp/tearDown hooks to create and destroy state for each test.
- Reset global state in a
beforeEachorsetupmethod. - Use unique names for files, database records, or keys to avoid collisions.
- Mock at the right level—prefer dependency injection over patching globals.
- Run tests in random order in CI to catch dependencies early.
If you have a large suite, you might also consider running tests in parallel, but that requires even stricter isolation.
Comparing Random vs. Fixed Order
Some teams prefer a fixed order (e.g., alphabetical) because it's reproducible. However, a fixed order can hide dependencies for a long time. Random order is a better practice because it surfaces these issues early, but it can make debugging harder if you don't have a seed. Many runners allow you to set a seed for the random order, so you can reproduce a specific failure.
If you're using a runner that supports it, run with --random-seed to reproduce the exact order that caused the 37 failures. That gives you a starting point for debugging.
Frequently Asked Questions
Why did only 37 tests fail and not all of them?
Only the tests that depend on shared state fail when the order changes. Tests that are truly independent will pass regardless of order. The 37 failures are likely the ones that rely on a particular test running before them.
Can I just revert the order change to fix the failures?
You can, but that only hides the problem. The tests are still fragile and will break again if the order changes in the future (e.g., on a different machine or with a new runner version). It's better to fix the underlying dependencies.
How do I find which test is causing the dependency?
Run the failing test alone to confirm it passes. Then, run it with each other test one by one (or use a binary search) to find which test's side effects it needs. Tools like pytest-ordering or JUnit test order plugins can help.
Is it okay to use a shared database in tests?
It's okay if each test cleans up after itself and doesn't rely on data from other tests. Use transactions or truncate tables between tests. The key is that no test should assume another test has already inserted data.
What if the failures are due to timing, not state?
Timing issues can also be order-dependent. For example, a test might pass when run after a slow test because it gives time for an async operation to complete. To fix, use explicit waits or mocks instead of relying on timing.
Should I use a test isolation library?
Libraries like testcontainers or pytest-datadir can help manage resources, but the core fix is to write tests that don't share state. Start by reviewing your tests for shared globals and side effects.