How Unit Tests Saved My Marriage
I’m not married, but if I was, they would have. Here’s a recent real-world(TM) experience:
- A co-worker found that a tool of mine died when writing data in a certain format.
- I checked out the latest version, ran the existing tests.
- The tests passed. Curious, but that’s given me an idea that it’s something I haven’t thought about.
- I read through the tests and found there was no test for the format he had tried. However there were tests for similar formats, and they passed. So, the scope of the bug was reduced, and myself and my co-worker had more confidence that my tool wasn’t completely broken.
- I added some tests for the format he tried and some similar other ones.
- Re-ran tests. The one he had a problem with failed the test! But the similar ones passed. Bug scope reduced further!
- With the bug scope reduced to something very small, I debugged the code and tried a fix.
- Re-run tests. Both new tests and old tests pass, so I have confidence that my fix does not break old behaviour!
- Now I can release with 99% confidence, and others have confidence in my tool. I also have more tests for when something like this happens again (which it will).
- Future wedding: Saved.
Time taken: 30 minutes.
Without tests, fixing this bug would have turned into an afternoon of making custom tests, poking the data, trying new bits of code, then releasing with no confidence that it does not break existing behaviour.