Monday, April 20, 2009

You don't have to remember anything when you write unit tests

I'm a fan of English rapper Mike Skinner (The Streets), he gives some good advice in his songs. For example:
If you never tell a lie to her, you don't have to remember anything.

That's easy right; you never have to be paranoid about creeping inconsistencies into your story if you just stick to the facts.

It's kind of the same with unit testing. Ever have the feeling 'I sure hope no-one will break this subtle case' when you're writing code with some twisted logic. You try to clarify it with comments, but still it doesn't feel right and it keeps hogging your mind. Well when you write a test you don't have to bother worrying anymore, the test will fail when you break the code and if you cover your subtle cases you're safe.

It is hard to convince developers to write unit tests. Some feel like writing tests is a waste of time. I felt the same way, until the first time the tests found errors that I did not expect. Every time a test fails is like a little gift to yourself; some fustrating hours have been saved thanks to the test harness.

No comments: