Having recently watched Kent Beck’s Test-Driven Development Design (TDD) screen casts available for purchase from the pragmatic programmer website, I must confess I’ve been converted.
I have been writing unit tests for a few years now, and prior to NUnit supporting .NET 2.0 I wrote my own unit test framework, a simple Test Runner which used attributes and reflection to identify and execute tests, an NUnit like GUI, and a static Assert class. I saw unit tests as a good means of asserting the correctness of code, of preventing the introduction of bugs during code changes, and an excellent means of documentation. Furthermore, it forces you to make incremental changes during refactoring and provides constant feedback during the change process. All good stuff.
Despite the benefits, I had two problems with TDD.
Firstly, I misunderstood it. I think it should be renamed Test Driven Design to emphasize that it really drives the design of your code. This is the crucial point that, somehow, I had missed. I had heard that it drives design, but I was not listening.
Secondly, it felt like a lot of rework. Not only did I need to keep re-coding classes, I had to keep re-working the tests. It was whilst watching Kent Beck working through his examples that I noticed his heavy reliance on refactoring tools. TDD and refactoring tools work together like the hammer and the anvil. You need both. Fortunately in C# there is an excellent plugin from JetBrains called Resharper which makes extracting interfaces, generating method stubs, etc. a piece of cake (a must have tool for C# development).
As mentioned, I’m converted.
Kent Beck begins his screen casts with the following introduction:
Test-Driven Development is designed to make developers take more responsibility for the quality of the code that they write.
It consists of three parts:
- Developers write automated tests as they go
- Tests are written in advance of the code
- Design a little at a time
He then proceeds to illustrate TDD by implementing a simple project, beginning with a high level list of tests he needs to write to code the required functionality.
It’s important to note that at this stage he is focused on objectives, what should this program do? Furthermore, he desires the fastest possible feedback so he picks tests he can quickly implement and build upon.
When writing tests Kent Beck uses very descriptive names for his tests. He likes to write little stories, “Every test should tell a story“. He also advises to be granular to get more accurate feedback. For example, if this is the test he wants:
@Test public void getRetrievesWhatWasPut(){
Tyrant t = new Tyrant();
t.put(key,value);
assertEquals(value, t.get(key));
}
He will break it up into smaller tests first to get the fastest possible feedback. In this example, what if Tyrant isn’t running? With that in mind his first test looks like this:
@Test public void getRetrievesWhatWasPut(){
// Tyrant t = new Tyrant();
// t.put(key,value);
// assertEquals(value, t.get(key));
new Socket(“localhost”, 1978);
}
He then mentions that eventually the tests should be self contained and should start-up Tyrant before executing, but at this stage he doesn’t want to invest time in that. He will do that later, and he adds that to his Todo list so it’s not forgotten. Right now he wants to validate his assumptions based upon his reading of the specifications. This is one of the most important lessons I learned watching Kent Beck, how to proceed in an orderly manner and decide what to invest time in, and when.
It’s one thing to read a book about TDD, it’s another to watch an expert demonstrate his craft, I highly recommend the screencasts to anybody interested in learning more about TDD. Here are the first 10 mins of episodes one and two:
TDD Intro, Episode 1, first 10 minutes, unedited from Kent Beck on Vimeo.
TDD Intro Episode 2, first 10 minutes, unedited from Kent Beck on Vimeo.

What a hilarious waste of time.
A perfect, “Emperor’s new clothes,” example.
Writing line after line of hex-level socket manipulation before (I presume, I didn’t pay to see the rest) replacing it all with a put() invocation.
In fact, that video is such a fine example of the lunacy of TDD that it might be worth buying it afterall.
What is wrong with these people?
Hi Shmoo,
Thanks for your feedback.
For me the videos were a big help, I had always thought of refactoring in a reactive/passive sense, ie. a methodology for implementing code changes, or improvements to existing code. I thought TDD was primarily a method for testing code, and protecting against introducing errors during the refactoring process.
Of course, it does help with preventing the introduction of bugs, and it provides good documentation – how do I use this class? Have a look at the unit tests.
I actually had the whole process back to front.
Seeing Kent work helped me understand that TDD is about design, asserting correct behavior, and that the use of refactoring tools was a very active part of the process. And you need both.
I believe BDD was initiated as an effort to correct people’s misunderstanding, and misuse of TDD.
As mentioned in the post, I’m converted. I’ve started using TDD as Kent demonstrates on my personal project and have never looked back.
@Shmoo
What exactly is your objection here?
Ok, you reckon it would be preferable developing in a different way.
Of course TDD might not be everyone’s cup of tea, and maybe different situations might suggest different approaches.
What would you have done, were you in Mr. Kent’s shoes?
I bought the screencast and found them very interesting.
The point, in my opinion, is: learning during the development, design and test phases.
I found it perfectly suitable to my style. It is just the next step after you discover unit testing: using it to “drive” your effort, starting to think bottom up in a new, pragmatic way.
Of course the same could be done using several other strategies, and maybe you think those would be more compatible with your thougt processes.
Anyway I don’t understand the dissing… it’s like you regard TDDers as zealots, some sort of a sect. Of course you always find that kind of people in every “group”, but I think TDD is more like craftmen trying to distill their best practices.
Hi Manrico,
Thanks for your comments, my thoughts exactly.
In those videos Kent really conveyed to me TDD driving design/development – as you mentioned. That was key for me, and watching his aggressive, methodical, use of refactoring tools.
Wisdom is in subtlety, and if you pay attention you can see him deal with those subtle issues – Should I invest time in this now? Where should I begin? How much should I test? All in an hour or two.
I thought the videos were excellent.
Hello, David. Many people have called TDD “test-driven design” over the years, but of course the original name stuck. In my work I routinely emphasise that I treat TDD as a design activity with testing as a side effect, although it took me a few years to really understand what this meant.
I hope you find TDD helps you build more responsive designs.
Hello J.B.
You are right, it really deceived me for a few years – that’s why I thought it was okay to add tests later, once the code was stable.
I completely missed the point! And to be honest, it makes some of my early posts look silly. I need to revisit some of my other assumptions about XP and Agile.
I’ve always cared about quality in code, thanks to those videos, I now see TDD as an essential discipline for delivering quality.
Thanks for your comments.
I watched the videos for the first time too.
I also found them valuable. The discussion of principles at the end of the fourth part was helpful for me. For my learning style I think it would have helped me if they were at the beginning.
One of my take-aways was that TDD is a practice, not a set of prescriptive, context-free rules. Which among other things means for me that what tests you end up with depend on your context.
Best,
Don
Sorry for the late reply Donald I’ve moved countries recently,
Yes, I agree with you, it is a practice and context is very important.
Actually, some things you can’t test with TDD, ie. some non-functional requirements.
Thanks again for your post.
Kind Regards
David.