Why Unit Tests Matter and How They Will Save You Time

Unit tests can actually save you time in the long run even if you (or your boss) don't currently use or believe in them


June 12, 2008
URL:http://www.drdobbs.com/windows/why-unit-tests-matter-and-how-they-will/208403755

Dan Wahlin (Microsoft Most Valuable Professional for ASP.NET and XML Web Services) is a .NET development instructor and architecture consultant at Interface Technical Training. Dan founded the XML for ASP.NET Developers site, which focuses on using ASP.NET, XML, Silverlight, AJAX, and Web Services on .NET and runs smartwebcontrols.com. He's also on the INETA Speaker's Bureau and speaks at several conferences. Dan has co-authored/authored several different books on .NET, including ASP.NET 2.0 MVP Hacks, Professional ASP.NET AJAX, XML for ASP.NET Developers and is currently working on a new book on Silverlight 2. Dan blogs at weblogs.asp.net/dwahlin


The point of the post is to explain why unit tests can actually save you time in the long run even if you or your boss don't currently use or believe in them. It's not my goal to go into some silly religious discussion about why unit tests should or should not be used in a project. There are plenty of forums out there for arguing over various technical concepts and methodologies if you have the time to waste.

Many different philosophies have been proposed that offer solutions for writing quality code. As a result, two people will generally give two different answers if you question them about the best way to write quality code. Regardless of your views on writing quality code, testing has to fit into the picture at some point. We could argue over exactly where testing fits into a project but I don't think anyone would argue that testing can be skipped (and if you're one of those people you can save yourself some time and stop reading now :-)).

I've never been one of those "letter of the law" people when it comes to just about anything and that applies to concepts like testing code as well. I believe balance has to be reached regardless of what you're doing. There is such a thing as going "overboard" when it comes to software development, studying for a test at school, training for sports or many other things. However, everyone's definition of "overboard" differs and I respect that so I'll move on to the heart of the matter which is unit testing and how it can benefit your projects even if you don't agree with the "letter of the law" people out there.

If you're new to unit testing, I'll sum up the general concept quickly: Write tests followed by code to satisfy those tests. That one statement doesn't do unit testing justice, but hopefully you get the basic idea. Do I always write my tests first along with the application's general stub code? Simple answer is "I try to". I try to follow best practices but in reality it doesn't always work out exactly how I want.

Regardless of your view on the overall process, unit tests can still provide significant benefits in my opinion even if you write them later in the development process. The "letter of the law" people probably consider that to be completely wrong, but I try hard to keep in mind that everyone and every project has different skill levels, needs and constraints. Whether you write your unit tests at the beginning, in the middle or even at the end of a project (pushing them off until the end is not recommended and your hand should be slapped with a ruler if you do that :-)), they can still provide you many benefits.

There are numerous Websites dedicated to the topic of unit tests. Wikipedia provides a nice overview here. A few of the benefits unit tests bring to the table include:

For some people writing unit tests before writing application code (more than just stub code) is absolutely required for writing good software. I agree with that stance overall since if you build testing into a project from the beginning then you should definitely have higher quality code in the end if you stick with it and ensure that you've achieved good code coverage with your tests. However, I've also been on projects where writing all of the unit tests up front simply wasn't going to happen due to the time constraints (which are sometimes ridiculously out of touch with reality) placed on a project. Regardless of your situation, unit tests can still help you in the short-term and long-term.

Here are a few reasons you should consider using them if you're not already. I've found that they actually save time in the long run if you spend a little time up front.

To sum things up, I'm a big fan of unit tests simple because I end up writing test harness programs at some point anyway, need to debug a production application or am forced into a situation where someone wants to make that one "simple" change in an application and I don't know the true impact. By using test frameworks such as Visual Studio, nUnit, xUnit.net, mbUnit, and the like (I personally use the features found in Visual Studio), I save myself the time of getting test data into the database, writing test harnesses, maintaining an application, plus much more. By writing unit tests are you going to catch all of the bugs? Obviously not...but you'll be well ahead of where you would have been without them.

Don't take my word for it though, start creating a few unit tests for an application you're already working on (even if the tests are created after the fact in this scenario) and you'll see how nice it is to see green lights as your tests pass or red lights when you need to fix something you wouldn't have caught otherwise. It sure beats pushing off all testing to your end users and wasting their time reporting bugs that you could have dealt with much earlier in the development process.

If you're interested in getting started with the overall concept of unit tests some nice starter articles can be found here.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.