Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Seven virtues of Test-Driven Development

, 23 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
The advantages of using TDD.

Don't get me wrong, this is not another "You should practice TDD because.." post. This one is aimed for those who want to start test-driving, but needs some guidance on how to get the maximum benefit and avoid the typical disasters. I plan to write several posts about various kinds of tests and when to (not) use them. Different kinds of tests (and testing practices) provide different benefits, and I thought maybe I write this as a cornerstone kind of post and refer to it from time to time.

So, these are the promised virtues, in chronological order.

Tests help you design the API of your class

You might have fancy ideas about your methods and properties, but the users who actually use your stuff think otherwise. Hate mail follows.

Be the first user of your class before you even write it, and you'll make it right before anybody else sees it.

Tests help you design the architecture of your system

You think you're smart. Those DML diagrams make your mama proud of you. Until you get back to your app after a year or two and have to make a couple of changes. You discover a couple of 2000 LOC methods, goto statements, and even Stored Procedures. Gods, what a mess!

There's no rigorous proof, but numerous sources tell us that test-driven design is much more flexible and maintainable. There are several reasons to believe that, and it's worth a separate post. Anyway, if you stop pretending you're fail-proof, and start listening to your tests, you might improve your design beyond your wildest dreams. This is considered the most important benefit of TDD, although not obvious from the beginning, since this is not directly related to "testing".

Tests help you implement the members of your class

Test-driven, your method eventually becomes The Simplest Thing That Works. Most of what we said above apply to member-level design as well.

With tests, you are sure that your application still works correctly after you change it

After making a little change to the underlying implementation of a particular minor feature, you manually test the whole system.. or your end users do that for you. Either way, you spend the weekend trying to get the corrupted data from a backup you accidentally made a couple of days ago. (I'm not making this up, it actually happened to me!)

Tests, when done properly, can be a safety net that protects you from such unfortunate accidents. You can freely refactor your code in order to further improve your design, and not be afraid of introducing a regression bug.

Tests document your API

When structuring your tests around classes (which is not the best idea, but is OK for a start), you can look at a particular test and understand what the corresponding method does. That is, if the test is named and written appropriately (I promise a post on this).

Tests document your system

This is not the same as the previous one. When looking at a new system, I'm trying to figure out how to do stuff with it, not what a particular class does. So, when structuring your tests around the features of the system, you provide a nice way of documenting its behavior, both "how to use FeatureX" and "what happens if I actually use it".

Is it seven already?

If you know other benefits, please, please write them in the comments!

OK, now what?

Hopefully you don't use TDD just because the Alt.Net guys told you to, but because you want to get certain sweet stuff out of it. Now that you know these benefits, you don't do it blindly. You try to write your tests so that they make refactoring easier, not harder. So that they actually influence your design, and not the other way. So that they are easy to understand, and make you understand the system even if you're a total stranger.

How do you achieve that? Oh, that a different story..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Artem Smirnov
Software Developer GeekSoft
Lithuania Lithuania
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionCareful with statement #4 Pinmembermartin_bisson26-Jul-11 8:51 
AnswerRe: Careful with statement #4 PinmemberArtem Smirnov26-Jul-11 9:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 23 Jul 2011
Article Copyright 2011 by Artem Smirnov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid