Why bother testing?

There have been quite a few posts here about testing, and I expect that there will be several more, but so far I haven’t addressed a fundamental question: Why bother testing?  It’s something that’s good for the soul but you don’t (usually) get paid for shipping tests, just for the production code and documentation.

To answer that question, I’ll take a brief detour: cleaning your teeth.  Very few people jump out of bed in the morning happy because they will be brushing their teeth that day.  In a few ways I think it’s similar to testing – something that you know you should do, but that doesn’t command your attention or enthusiasm most of the time.

I’ve heard of a dentist who told a patient: you only need to brush the teeth that you want to keep.  I think that you could go further: you only need to brush the teeth that you want to keep and be free from excruciating pain.  Ah, that shifts perspective quite a bit.

So, returning to testing: you only need to test the code that you want to work.  Unfortunately, in real life it’s a bit woollier and clumsier than that: you only need to test the code in which you want more confidence that it works.  (Testing won’t tell you that your code works and will always work.  It will increase your confidence that it works, and you can test until your confidence has reached a level you’re happy with.)  But I hope that you’ll remember the pithier version, and have the nuance in the back of your mind.

Actually, I think that testing has other benefits for programmers.  If you work in a team, you can’t always easily tell what state a bit of code is in before you start work on it.  It’s a good idea to check that you have a solid foundation, i.e. the code’s in a good state, before you start building on top.  Otherwise, if you hit problems part-way through, you won’t know if it’s a problem you’ve caused or one that was there already.

Testing can help with that.  The 0th test is: does it compile?  After that, seeing a lot of green test results is also very nice.

Another benefit is that it gives you confidence to do big things like refactoring your code.  If the tests still pass, your refactoring is safe (as far as the test can tell).

If you combine testing with automation, then lots of other things open up like Continuous Deployment and Continuous Delivery.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s