Since I recently finished reading xUnit Test Patterns and my upcoming webinar is on Unit Testing, I have been thinking a lot about it lately. I thought I would expand on some of the ways in which I find unit testing incredibly useful.
Most of wouldn’t even think of doing something crazy like climbing El Capitan without a rope or working high up on a cell tower without some sort of safety gear or safety net. We realize the danger there. Even if we think we are not going to fall, the consequences of falling would be very catastrophic, so we mitigate that by using safety systems.
We can apply the same concept to refactoring. Anytime we modify code (especially working code), we run the risk of inadvertently breaking something else. Sure we use Source Code Control (SCC) – at least I hope you all do, so we can always roll it back when we detect a problem. The real question is: “How do we detect problems?” and “How do we detect them in a timely manner?”.
Source Code Control and Unit Testing work in tandem to act as a robust safety net. The problem with just using SCC is that we may not catch the problem right away or more embarrassingly our customers may catch it for us. When we do eventually catch the bug we’ve introduced (it’s bound to happen sometime), we now have to sort through several commits full of changes to determine what caused the problem.
If you have Unit Tests, when you find the bug you can run them and they should pinpoint the cause of the issue. We can take this one step further and avoid sorting through all the commits by running the Unit Tests before every commit. We can simply discard our changes and try again. That way we spend less time debugging. Less time debugging means more time for valuable activities like designing and adding new features.
If you are not currently not using unit tests you are swinging on a trapeze without a safety net. Sure, you might not fall, but what would happen if you do? Isn’t worth taking a little time to at least look into Unit Testing? Come to our Unit Testing Webinar on Wednesday and you’ll see it’s not as difficult as you think to get started with Unit Testing. Start building your safety net. One day you will be glad you did.