I wrote integration tests for pure logic because I built layers you couldn't mock

We recently audited the test suites in every part of our system: the backend service, the agent that runs on worker machines, the web client, and the shared contracts library. The results were bad, and most of the cause is a habit of mine. I wrote a lot of this code over many changes. For every layer above the lowest one, I wrote the code so that its tests could only run against the real layer underneath it. As a result, nearly every test became an integration test, including tests whose only job was to check pure logic.

This post covers what went wrong, what it cost, and what I do differently now.

What I did

The goal was simple and I knew it. Most code should be covered by real unit tests, each testing one thing with its dependencies mocked. Integration and end-to-end tests should cover the paths a user actually takes, both happy and unhappy, but they shouldn’t make up most of the suite.

I didn’t write code that allowed that.

Backend. The service’s shared state held a concrete handle to the data store. The store had several hundred methods and no interface. That meant any test of an HTTP route had to clone a real database first, even when the test only checked that malformed input returned a 400 or that a missing route returned a 404. Hundreds of tests did exactly that: parse and routing checks that needed no data at all, each one paying for a database setup. On top of that, I tested the same behaviour again at each layer. The scheduling rules already had fast unit tests over the pure planning function. I asserted the same decisions a second time through a database-backed scheduler, and a third time from the API layer.

Agent. The code that runs after a work session built its own version-control helper inside the function and called the real landing step directly, so a test couldn’t swap in a fake. Simple decisions, like which outcome follows from a given status, which branch to target, or whether to hold a landing, could only be tested by running a full landing across several real repositories. The terminal multiplexer wrapper was a concrete type, so every terminal test started a real server. One test fixture file even stated a “no stubs” rule for external tools. I followed it without asking whether it was right.

Web client. Screens fetched their own data through global store hooks that went to the real API client. The only place a test could intervene was a stubbed global fetch. So I tested wording, grouping, and ordering by mounting the whole application. One screen had more than three times as many whole-app tests as model-level unit tests. On top of that, the browser matrix ran every test file in six engine and viewport combinations, including tests of logic that has nothing to do with the viewport.

Contracts. This layer was done right. Its tests are true unit tests and the whole suite runs in about two seconds. It has one gap: the contract types are written twice, once per language, and no test checks one copy against the other.

What it cost

  • Runtime. On the backend, about two thirds of the tests paid for a database setup, and those tests accounted for about 98% of total test time. The setup itself took a quarter of a second before any assertion ran. Spread across thousands of tests, that floor was the runtime. The pure tests ran in under half a minute combined, while the pipeline step took close to half an hour. The client’s jsdom step took 42 minutes of a 90-minute job. A full pre-landing check across all repositories took about an hour, and a queue of pending changes sat waiting on it.
  • Brittleness. Because the same behaviour was asserted at every layer, a change to a lower layer broke tests in every layer above it.
  • Bloat. Every layer above the core had more test code than production code, in one case about twice as much. The tests had become the larger thing to maintain.
  • Weaker coverage. Large, slow integration tests built around fixtures don’t show you which decision is wrong. They show you that a whole stack went wrong somewhere.

What I should have written

Each upper layer needed a seam, a point where the layer below could be swapped out. Each pure decision needed a unit test at the layer that makes it.

  • Backend: an interface, or an injected handle, over the store, so route tests run against an in-memory fake. Then parsing, validation, and route-shape tests need no database. The scheduling rules get tested once, against the pure planner. Database-backed tests cover only the wiring: one success path and one failure class each.
  • Agent: an injectable landing step, an interface over the terminal multiplexer with a fake implementation, and a scripted stub for the VM tooling. Then decisions can be tested as plain functions of their inputs.
  • Client: screens get their data through props or an injected store. Wording, grouping, and ordering get tested through each screen’s model, and the whole-app tests cover one happy path and one unhappy path per screen. Tests that don’t depend on the viewport run in one browser configuration, not six.
  • Contracts: one language emits fixtures that the other language’s tests read, so the two copies of each type are checked against each other on every run.

How I’m changing

We added a test-layering standard to our coding guidelines. Two rules from it are now how I work:

A layer that an upper layer needs to mock and cannot is a structural defect. Give the upper layer a seam, such as a trait, an injected function or a component taking its data as props, rather than testing it through the real lower layer.

Pure logic, such as parsing, validation, mapping, ordering, wording and policy decisions, is a function of its inputs and is unit-tested as one. It is never reached through a database, a process, a socket, a router or a whole application to be tested.

In practice, I now use these checks, in reviewing my own work and other people’s:

  1. “What would this test need if the layer below were fake?” If the answer is “nothing”, the test belongs one layer down. Checking a status code, a parse error, an ordering, or a label should never require a database, a process, a browser, or a full application.
  2. Look for missing seams when reviewing code, not when reviewing tests. If a struct holds a concrete handle to the layer below with no interface or injection point, every test above it will be an integration test, so I flag that at design time.
  3. Treat a test-to-code ratio above one, or the same test name in two layers, as a warning sign. Both usually mean behaviour is being asserted once per layer.
  4. Multiply the fixture cost by the number of tests that pay it. The slowest single tests weren’t the problem. A quarter-second floor paid thousands of times was.
  5. Question test-policy rules before following them. A “no stubs” rule in a fixture file is a decision that should be weighed against the standard, not adopted silently.

The rework is now tracked as a set of prioritised issues across all four codebases. It will take a while. Many of these tests have to be moved down a layer or deleted, and the seams have to be added to production code first.

The lesson for me: testability is part of a layer’s design. If I can’t test a layer by itself, the problem is in how I structured the code, and no amount of test infrastructure fixes that.

Advanced search: filter by model or tag