Founder Notes
I Wrote Down The Wrong Answer And Made It Correct
A mapping was producing the wrong output. A small thing, one field landing somewhere it did not belong, the sort of defect you repair in a minute once you can see it. What slowed me down was the test.
There was a test covering that mapping, and it passed. It had been written while the defect was already present, and it asserted the output the code was actually producing. Not as a placeholder, not with a note beside it. As the expected value.
Before I understood any of this I had read that test as evidence. It covered the mapping. It passed. So the mapping was fine and my problem lived somewhere else.
That was wrong, and not in the obvious way. The test was not failing to notice the defect. The test was holding the defect in place.
An assertion is not a measurement
Here is the part I had never put plainly. I think of a test as something that observes the code and reports back: this behaviour is right, this one is not. Under that picture a test is a measuring instrument, and an instrument pointed at a defect that says nothing is simply a poor instrument.
That is not what an assertion is. An assertion is a statement of what the behaviour ought to be. It does not describe the code, it makes a demand of it, and the demand is whatever somebody wrote down at the moment they wrote it.
So when a test is written while a defect is present, and it asserts the output as it stands, something changes that has nothing to do with coverage. The behaviour acquires a document on its side. It stops being a thing the code happens to do and becomes a thing the code is required to do, recorded as such, in the place everyone looks to find out what is required.
That carries a consequence I find genuinely uncomfortable. Changing the mapping to produce the right output now means contradicting an artefact saying the output it already produces is wanted. And the natural reading of a contradicted test is that the change is wrong, because that is what a contradicted test usually means. The defect has an advocate. The advocate sits beside the repair, written in the same voice, by the same people, carrying the same authority.
The dangerous moment is the writing, not the failing
I had been locating the risk around a test at the wrong end. I worried about tests that were too weak, that covered too little, that passed where they should not have. Those are real, and they are the forgiving kind: a test that misses a defect leaves you exactly where you already were.
The expensive kind is a test written confidently about behaviour nobody had independently established. At the instant it is written, my current understanding of what the system should do stops being my understanding and becomes the system's requirement. If that understanding was right, this is precisely the value of testing and the reason to keep doing it. If it was wrong, I have not recorded a mistake. I have promoted one.
So the care belongs earlier than I had been putting it. Not when a test fails, which is when I am paying attention anyway. When I write the assertion, and I have to answer a question I had been skipping: do I know this is what it should do, or do I only know this is what it does?
The second one is worth writing down too. It is just not worth writing down as the answer.