Being saved by...unit tests

The time comes in every developers life when a higher up requests what they think is a minor change. You think about it and agree also thinking that it would not take too much time. It is only when you get back to your desk, check out the code and look to where the change is going to happen when you realise the worst. If you make a change here what repercussions will you introduce, is it possible you could introduce a defect in the code which could then go on to really crap up the application. This happened to me last week, luckily I already had some unit tests in place covering the code which was in need of changing. I knew what the method in question was going to return, and so did the unit tests. I made the necessary changes and re-run the tests. Fantastic they all pass. The code checked in, the tests passed again and the nightly build was successful. The moral to this little tale? Introduce unit tests as soon as you can to test your code. As Ian Cooper points out:-

"The problem is that the most important step is not doing it right, but doing it at all."

So if you are writing some code, stop have a think and throw some unit tests in first. Even if you think they are not of a good standard, it doesn't matter. If your tests are simple and easy to understand and they pass the method they are testing, excellent work. Give yourself a pat on the back because 6 months down the line when a higher up asks you to change that method, you can go home in the knowledge that the unit test is working for you.

No Comments