The bug is already fixed.
You found it.
You understood it.
You changed the one line.
Now comes the part everyone skips.
Write the test that would have caught this.
Not later.
Not when there's time.
Now, while you still remember
exactly how it lied to you.
A fix without a test
is a promise with no memory.
It works today.
It forgets by Friday.
Someone refactors the file
and the same bug walks back in
through the same open door.
The test is not paperwork.
It is a trap you set
for the exact mistake
that already fooled you once.
Most tests are written for code that works.
The best ones are written
for code that just failed,
in the shape of the failure,
so it can never fail that way unseen again.
Reproduce the bug in a test first.
Watch it go red.
That red is proof
you are testing the real thing.
Then fix the code.
Watch it go green.
That green is the bug's tombstone.
A test that never failed
proves nothing.
It only decorates the suite.
The one that failed before your fix
is the one that will guard the door
long after you have forgotten
there was ever a door there.
Do not test to raise a number.
Test to encode what you learned,
so the next person,
so the next you,
inherits the lesson
without paying for it again.
Every hard bug
is a gift wrapped in frustration.
The test is you
keeping the gift.
The code says what it does.
The test says what it must never stop doing.
Write that second sentence
while the pain is fresh.
It is the cheapest insurance
you will ever buy,
and you are already
standing at the counter.
– Serguey Asael Shinder
Top comments (0)