The Art of the Test
Jeremy McEntire
VP of Engineering6 min read
You rename a field. Nothing about what the system does has changed. The build goes red. Forty-one tests fail, and you spend the afternoon teaching them the new name of a thing they were never really looking at.
Everyone who has written software for money has had this afternoon. Most of us have had it more than once, and the usual lesson drawn from it is that tests are a tax. I think the lesson is different. I think those forty-one tests were never tests at all. They were a photograph of the code, and we had confused the photograph with the thing.
I've long held that the mark of art is its capacity to evoke: to produce a response, to move someone to act, to shape what they believe. Lewis Carroll's Jabberwocky is my favorite case.1 Half the words are invented. Nobody can tell you what a tove is or what it means to gyre. And yet it conjures the same thing in nearly everyone who reads it: the dark wood, the boy, the blade, the father's relief. Carroll got the meaning without the vocabulary. The meaning, it turns out, was not living in the words.
Which raises the question the Greeks got to first. Theseus sails home and the Athenians keep his ship, replacing each rotten plank as it goes, until not one original board remains.2 Is it still his ship? Picasso asked the same thing with a bull. Eleven lithographs, ending in a handful of lines.3 Nobody looking at it would say it is less a bull. Some would say it is more.
So: what can you remove, alter, or replace without changing that which defines the thing? Surely not every atom must sit just so. One could imagine an entirely different work, with different words and different lines, that lands in the same place. Something remains invariant across all the versions that work and is absent from all the ones that don't. The artist's job is to find it. The critic's job is to notice when it's gone.
That is also the tester's job, and we have mostly forgotten it.
A test that asserts the incidental shape of the code (this helper calls that one, with these arguments, in this order, when nobody outside the module cares) is a test of the photograph. It fails when the code changes and passes when the code is the same, which is to say it tells you nothing you didn't already know from the diff. Worse, it binds the implementation in place. Every refactor becomes a negotiation with a suite that liked things the way they were. The suite is not protecting the user from bugs. It is protecting the past from the future.
Sometimes the order is the requirement. Authorize before you charge. Then the order is the invariant, and you assert it without apology. The sin is not asserting order. The sin is asserting order that was never promised to anyone.
Mocks are where this goes to seed. Mock the database, mock the clock, mock the network, mock the colleague's service, and what remains is a test that verifies your assumptions about all four are consistent with one another. It is a sentence that asserts its own grammar. A mocked clock can catch a real expiry bug; a mocked clock that never advances cannot. The problem was never the mock. The problem is mocking away the very behavior you claim to have verified. Feynman's verdict on the Challenger applies: nature cannot be fooled.4 A green suite that has mocked away nature has not fooled nature. It has only fooled you.
The better test asserts the invariant. Not "the booking service calls pricing and then the ledger," but "a guest who pays is charged exactly once, and the host sees it." Not "the function returns this struct," but "every line is the price we quoted times the nights the guest chose, and the total is their sum." The second half of that matters. A total that equals the sum of its lines is satisfied by zeroes all the way down. The invariant has to reach outside the code to something independently agreed, or it is just the code agreeing with itself. Write it that way and you can replace every plank. You can rewrite the pricing engine, swap the database, move the whole thing to another language, and the test still has something to say. It is the test as Picasso's last bull: the fewest lines that are still unmistakably the thing.
This is harder than it sounds, and the reason it's harder is the reason people don't do it. Pinning the implementation is cheap. You look at the code and write down what it does. Finding the invariant means knowing what the code is for, and that is a question the code will not answer. You have to go ask the business. You have to think. And so, as with most things, the cheap practice spreads not because anyone believes in it but because it asks less of us on a Tuesday afternoon and sends the bill some Tuesday later.
Pirsig spent a whole book chasing the word Quality and mostly declined to define it, on the grounds that you know it when it's there and you know it when it's gone.5 He was right about motorcycles and he's right about test suites. You can feel the difference between a suite that guards intent and one that guards the status quo. One lets you move. The other makes every move expensive, until someone proposes deleting the tests, and the someone is usually right.
The ship is the ship because it still carries Theseus home. The poem is the poem because the dread still gathers in the wood and the relief still breaks on the last page, whatever the words. The test is a test when it tells you, after everything else has changed, whether the thing is still the thing.
Everything else is a photograph. Keep a few. The ones at the boundary, where another team or another company has built against the shape, are worth keeping; there the shape is part of the promise. The rest are souvenirs. Don't mistake them for the work.
References
- Jabberwocky (Carroll, 1871)
- Life of Theseus, chapter 23 (Plutarch, translated by Perrin, 1914)
- The Bull, the eleventh state (Picasso, 1946), Art Institute of Chicago
- Appendix F: Personal Observations on Reliability of Shuttle (Feynman, 1986)
- Zen and the Art of Motorcycle Maintenance: An Inquiry into Values (Pirsig, 1974)