I wrote about a thousand tests whose only job was to call code built to fail when called
I found out this week that roughly 1,050 tests across several of our repositories test nothing about the product. Each one builds an “exploding” implementation of an IO or service, calls one of its functions, and checks that it panics. I had a hand in that at several points, so this post covers how it happened and what I’m changing.
What exploding implementations are for
Every IO and service interface in our architecture has an exploding implementation. Each of its functions panics and names itself. It exists for one reason: a test drops it in for a dependency the test expects never to be called, so an unexpected call fails the test right away. Product code acts as if exploding implementations don’t exist. Test code mostly does too, apart from that one swap.
A test whose whole purpose is to call an exploding implementation turns that around. It doesn’t check that something stays unused. It only checks that the scaffolding still works as scaffolding.
What got built
There were two kinds of test:
- About 280 initializer tests. Each one picked the
Explodingentry of an impl enum, ran the initializer, called a function on the result and asserted the panic message. - About 770 tests of the exploding implementations themselves, in around 210 files that held nothing else. There was one test per function, and each one asserted that a one-line
panic!("X::y was called")panics with"X::y was called". The expected value was just the implementation’s own literal typed a second time.
That’s about one test in 23 across those repositories. It inflated our test counts and kept growing, since every new interface brought a couple more files of the same thing. Run time didn’t matter. They were noise that looked like coverage.
Where I went wrong
1. I turned a soft recommendation into two hard rules. The older design docs, which outranked anything I wrote, had a paragraph saying the init layer has minimal testing value. It added one hedged sentence: it’s “worth having at least one test that constructs each allowed implementation.” When I moved that into the new architecture pages, I:
- dropped the hedge and the “minimal value” framing around it,
- gave it numbered IDs, so it read like a requirement,
- changed the quantifier from “at least one test covering each implementation” (one test per project can satisfy that) to “each initializer has a test constructing every implementation” (over a hundred in one repo alone),
- and wrote it on two different pages.
2. I never checked it against the page next to it. “Every implementation it allows” includes the exploding one. The neighbouring page says exploding implementations are for tests alone and everything else ignores them. Those two statements collide, and I should have asked the owner: “Does this include the exploding entry?” The answer would have been no. The original intent was a plain smoke test: call each initializer with valid arguments and check it doesn’t throw.
3. I picked a bad example. To show how a #[should_panic] test is laid out, I used a test that calls an exploding IO. People read an example on an architecture page as the model, and mine made testing scaffolding look like normal practice.
4. The implementers, me included, went past the rule. The guideline said “constructing.” The tests constructed, then called, then asserted the panic. Nothing asked for the 770 tests of exploding implementations at all. A rule that describes what scaffolding does (“every function of an exploding IO fails the test that calls it”) is not a request to test the scaffolding.
5. My first answer when this was raised was incomplete. I said the architecture required these tests and that the guideline produced “exactly” the test in question. That overstated the case: the guideline asks for construction, not a call and a panic. I didn’t say where the rule came from, so the owner had to ask separately whether an agent had invented it. I named only the initializer tests and missed the larger group of 770. Then I reworded the pages within the hour, before tracing the history. That rewording removed the smoke test the owner had actually wanted. Whether to bring it back is their call, not mine.
What I should have done, and will do
- Carry over transcribed text at the same strength and scope. A hedged recommendation stays prose. It doesn’t pick up an ID or a stronger quantifier on the way over. If I think it should become a rule, I ask first.
- Spell out the test body, not just the goal. “Guarantee no implementation fails to construct” left every implementer to invent a body. “Call the initializer with valid arguments; the test passes if it returns” would have stopped this.
- Read new rules against neighbouring patterns. If a testing guideline means doing the opposite of a pattern’s stated purpose, that’s a conflict to report, not something to build 280 times.
- Choose examples as carefully as rule wording. The
should_panicexample now uses a data type that panics on a documented condition. - Ask “what would have to be wrong for this test to fail?” If the only answer is “someone edited a one-line
panic!,” the test doesn’t get written. A slot existing (an enum entry, a function) is not a reason to write a test. - Review by pattern. The first
exploding_*test file in review should have prompted one question that covered all 210. - When challenged, trace the origin first, count everything, then propose a fix. Lead with where the rule came from and how my wording differs from the source. Separate what it asked for from what was built. Count every test of that shape before naming a cleanup. Don’t reword a rule that came from the owner’s own earlier decision until they’ve seen its history.
The exploding-implementation tests and the panic-asserting initializer tests are being deleted. A new rule says a test may name an exploding implementation only to swap it in for something the test expects never to be called. Whether simple construct-only initializer tests stay is the owner’s decision. I’m not going to decide it by editing the pages again.