CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo (.com)

Blog moved to www.jeffreypalermo.com

TDD speeds up unit testing - level 300

Before reading this post, I recommend reading a previous post of mine along with the comments and trackbacks to related posts.

First, you may wonder why I'm contending that TDD speeds up unit testing if TDD itself stresses unit testing so much.  One can unit test without employing TDD.  I can write some code and write a unit test for it.  If I use this method, I'm likely to have a hard time unit testing the code because it may call out to a database or the file system or a web service or some other object that is difficult to create in isolation.  If I think about how I'm going to test the code as I'm writing it, I'll end up with code that's easier to test because I'll depend on interfaces and employ other techniques to ensure my code is loosely coupled.  Because my goal was a piece of code for which it was easy to write a unit test, I'll end up with code designed for that.  At this point, I'm blurring the lines between just unit testing and TDD.  I didn't write my unit test first, but I _thought_ about how I would write my unit test first.  I imagined the method of testing before writing the code.  From this point, it's a snap to then actually write the test that's floating around in my head before coding the production code.

From a pragmatic sense, it probably doesn't matter which code is actually typed first if you've already decided on the unit test in your head.  You've written the unit test in your head before the production code.  The typing is then just semantics. 

If you aren't thinking about unit testing at all (if you don't currently do unit testing), then this whole topic is worthless for you, but if you are attempting to write unit tests after you design your code, I'm sure you can relate that it's often difficult because you have to pull in other classes just to test a single class.  If this happens, then it's not a unit test at all.  It's an integration test because it tests multiple things.  The difference in unit testing and integration testing is a topic for another post, but the difference is very important, and if the difference is not understood, then arguments for/against TDD don't make much sense at all.

The above is how TDD speeds up writing unit tests (if you have already been doing unit testing).

If the goal is code with unit tests, then the code will be designed for easy unit testing.  To design for easy unit testing, a developer has to think about the test first.  This leads to mental TDD, and after the code is designed, the unit test can be written quickly.  The next step would naturally be to just go ahead and type the test first instead of keeping it in your brain.  If at this point you prefer to continue to type the test after the production code, go ahead.  You will have benefited some by focusing more on how to test the code.


Comments

Jeffrey Palermo [C# MVP] said:

If a class is well-designed, it will be easy to unit test.  Good
design speeds up unit testing,...
# April 12, 2006 11:51 PM

Kai said:


Here is a post.
http://www.lhotka.net/WeBlog/CommentView,guid,8b4ed349-f885-4640-9221-1c29a3746499.aspx

In short, please take a look of the show, and give some comments on the show: http://perseus.franklins.net/dnrtv/0013/fnet.html
# April 14, 2006 8:48 PM

Kai said:

sorry, a friend of mine told me the TDD show link is somehow messed up, this is the link:

http://dnrtv.com/default.aspx?showID=10

or,

http://weblogs.asp.net/cfranklin/archive/2006/03/16/440359.aspx

# April 14, 2006 9:07 PM

Vikas Kerni said:

Rocky made one more controversial statement on TDD on Apr 19th, CNUG, IL

http://www.lhotka.net/WeBlog/CommentView,guid,8b4ed349-f885-4640-9221-1c29a3746499.aspx
# April 22, 2006 6:02 PM

Sam Gentile said:

Again another month and change since the last one so this issue will be a collection of everything marked...
# May 17, 2006 11:30 AM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication

News

Headspring Systems

View Jeffrey Palermo's profile on LinkedIn

See my new blog at .jeffreypalermo.com