~pperalta

Thoughts on software development and other stuff

Tod Golding on TDD

with 2 comments

At the SD East conference a few weeks ago they were giving away stacks of magazines. One of them was Better Software, a magazine dedicated to software quality. One of the articles was “Tapping into Testing Nirvana” by Tod Golding. He writes about his experience with TDD. As many of us that have adopted TDD, he was quite enthusiastic at the beginning, especially as the quality of his software improved. After a while however he noticed that the tests were getting more and more difficult to maintain. As Kent Beck says in Test-Driven Development by Example,

“Test driven development is a way of managing fear during programming.”

That turned out to be the exact opposite of Tod’s experience. He writes,

“Every time I wanted to tweak my code, I found myself worrying about how many tests I’d have to rewrite or simply throw away. I felt like I was continually building and destroying my suite of tests. I expected this at some level, but it still seemed like far too many of my tests were coming and going.”

He comes to the realization that code smells can happen in test code just as easily as production code. For those that are new to TDD, writing a test is simply a means to an end: the working code. Most of the TDD examples that one encounters concentrate on the xUnit testing APIs and techniques (such as mock objects.) However, to realize the full benefit of TDD the test code has to be maintained as much as the production code.

This, of course, led me to the real epiphany. I realized that those extracting the most value from their tests were treating them with much more reverence than me. While I always viewed my tests as shallow, one dimensional dolts that could be thrown away at any second, other developers were treating their tests like first-class citizens.

I am certainly guilty of viewing tests as shallow. My biggest sin in this arena is copy and paste unit testing. When I’m testing a method multiple times with different sets of parameters and conditions, the quick fix is to just copy an existing test method, make a few tweaks and rename the method. This of course makes the test class quite difficult to read and maintain. When changes get made to the production method, all of a sudden a bunch of test methods have to change.

As I looked more closely at their tests, I discovered that the best unit testers were applying all the same values to their unit testing as they were in their own implementation. They were abstracting out concepts, building object hierarchies, leveraging polymorphism, limiting coupling, and so in. In essence, their test code and their application’s code were on equal footing.

This view, in my opinion, represents the next step in TDD. Most people that have been on the JUnit website are familiar with the term “Test Infected”. This is the point where developers realize the immense value of a complete test suite and thus start to write unit tests. Next comes the practice of writing tests before code. Once the tests are being written before code, the next step is to view the unit tests as “first class citizens” and to apply the same concepts of clean code to the suite as to the production code.

Written by Patrick Peralta

October 4th, 2006 at 5:51 pm

Posted in Development

2 Responses to 'Tod Golding on TDD'

Subscribe to comments with RSS or TrackBack to 'Tod Golding on TDD'.

  1. Um, so does that mean we should start using TDD for our test cases? If you’re not supposed to refactor without test cases to back you up, how do you refactor your tests as they change?

    Obviously I’m being fairly tongue-in-cheek, but the underlying question is very real: Because we can’t treat test code “just like” production code, what are the differences and how do we manage them?

    Jim Moore [Visitor]

    5 Oct 06 at 8:39 am

  2. I think it means that we should give more care to the test suite. Obviously you won’t use TDD for the tests themselves, but it does mean refactoring out duplication, using inheritance and polymorphism where it makes sense, etc. The main idea is to not allow the tests to become a hindrance to change.

    As mentioned before, I’m guilty of copy-and-paste code, magic numbers, etc in my test cases, so this will be a bit of a transition for me. I’ll let you know how it works out :)

Leave a Reply