Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Haacked on TDD and Jeremy's First Rule of TDD

Unit Testing Loves Beta Testing And Vice Versa

Phil Haack has a great post up in response to some folks unhappy with their unit testing experience.  I think he makes several good points, including:

  1. Unit testing is only one layer of testing (duh)
  2. Automated unit tests are much, much simpler to write when you're writing the tests upfront and purposely creating testability

Like many people in the blog trail is saying, there are some types of code that are just too difficult for automatic tests.  User interface code is often an example, both heavy clients and web applications.  Testing the actual presentation code is difficult (but certainly not impossible).  Before you throw the baby out with the bath water and abandon TDD altogether for UI projects to rush in new features, consider this:

Jeremy's First Rule of TDD - "Isolate the ugly stuff "

Take the things that are truly difficult to test automatically and wrap them behind abstracted interfaces and separate any other code and functionality away from the difficult to test code.  Typically I think this would be access to external systems, user interface windowing, or anything involving a web server.  A picture perfect vision of my Grandmother boiling turkey bones after Thanksgiving comes to mind.  The bones aren't very edible, but you can do something to get the rest of the meat off the bones for endless post-Thanksgiving sandwiches. 

Circling back to user interfaces, the Model View Presenter pattern is a perfect example.  Actual user presentation code (System.Web.UI and WinForms) is harder to test, so make that code smaller.  Slice the "ugly" View classes as thin as possible.  Move the user interface flow logic into "plain old object" classes (Presenter) where it's much easier to test.  It probably is more code, but it'll make the UI code much easier to test and therefore easier to make work.  Since UI flow control code can become very complex and scary to change, it's awfully nice to be backed up with the automated unit tests.  You might decide to forgo unit testing the actual UI view code and call it a tolerable risk, but at least the controller logic will be unit tested.

The other example that comes to mind is a system I did last year that had some custom Active Directory queries in the authorization code.  We punted pretty early on trying to write automated unit tests for the actual Active Directory access code.  Instead, we put the AD access code into a thin Gateway class and interface so we could test the rest of the authorization logic without stepping into the Active Directory queries.  The only place that had any actual interaction with AD was the Gateway class itself. 

I would say that a lot of doing TDD is learning how to divide the class responsibilities up in a way to make the code easier to test (3 years so far and counting).  The fact that you often write code differently just for easy unit testing doesn't bother me a whole lot because it contributes to finishing the code sooner.  You can't just write code without regard to testability and expect to be able to retrofit unit tests on later.  I made the classic newbie mistake of trying to just write the code, then write the unit tests later only to find that my code didn't allow for easy unit tests.  TDD goes a lot smoother as soon as you realize that you need to change the way you build and structure code to take advantage of TDD.  Learning to write the unit tests first helps a tremendous amount too.  I think the book on best TDD practices is still being written, but there's a lot of experience out there already.  Before you throw your hands up in frustration over TDD, take a look at how other people are writing code for testability. 

P.S. - I don't know about Swing, but you can efficiently TDD a great deal of WinForms client code.  Maybe not everything is worth an automated unit test, but anything you can do helps.  Even without using something like NUnitForms a lot of WinForms classes can be treated as just "POO" and tested in NUnit/MbUnit without too much hassle.


Posted 10-20-2005 9:52 PM by Jeremy D. Miller

[Advertisement]

Comments

Jeffrey Palermo wrote re: Haacked on TDD and Jeremy's First Rule of TDD
on 10-21-2005 5:47 AM
Good points.
Carlton wrote re: Haacked on TDD and Jeremy's First Rule of TDD
on 10-21-2005 12:04 PM
Recently, I have been using a Mediator pattern to handle all the state changes in my UI and it has worked very well. How it works is I have all the UI controls registered with the Mediator at the Form\Page load event. Then when some event happens (or a state change), we talk to the Mediator NOT the actual UI controls. It is VERY testable and the tests are SUPER fast, as opposed to some NUnitAsp tests I have elsewhere in the app.

One thing I have noticed is that when you use Mediator is ALMOST leads you to a Command pattern. I would say the natural progression is a Command pattern, but I don't like forcing a pattern and the commands are not reusable with other UI controls.
Haacked wrote re: Haacked on TDD and Jeremy's First Rule of TDD
on 10-21-2005 12:43 PM
I LOVE your first rule! What's your second? ;)
Jeremy D. Miller -- The Shade Tree Developer wrote Mock Objects and Stubs: The Bottle Brush of TDD
on 12-19-2005 7:38 AM
I’m in a dry spell for blogging, so here’s a rehash of a presentation I gave internally at work earlier...
Jeremy D. Miller -- The Shade Tree Developer wrote Mock Objects and Stubs: The Bottle Brush of TDD
on 12-19-2005 10:49 AM
Author: <a href="blogs/jeremy.miller/">Jeremy D. Miller</a><br>You’ll frequently hear teams say they didn’t write unit tests for a particular area of the code because it was just too hard to test. I think one of the biggest challenges of using TDD is learning strategies for isolating the code that’s hard to test and writing code that is easy to test. One of the primary strategies to extend unit test coverage into those hard to reach places is to use mock objects, stubs, or other fake objects in place of the external resources so that your tests don’t involve the external resources at all.
Jeremy D. Miller -- The Shade Tree Developer wrote Mock Objects and Stubs: The Bottle Brush of TDD
on 12-20-2005 5:24 PM
Author: <a href="blogs/jeremy.miller/">Jeremy D. Miller</a><br>You’ll frequently hear teams say they didn’t write unit tests for a particular area of the code because it was just too hard to test. I think one of the biggest challenges of using TDD is learning strategies for isolating the code that’s hard to test and writing code that is easy to test. One of the primary strategies to extend unit test coverage into those hard to reach places is to use mock objects, stubs, or other fake objects i
Jeremy D. Miller -- The Shade Tree Developer wrote Mock Objects and Stubs: The Bottle Brush of TDD
on 01-14-2006 10:24 AM
Author: <a href="blogs/jeremy.miller/">Jeremy D. Miller</a><br>You’ll frequently hear teams say they didn’t write unit tests for a particular area of the code because it was just too hard to test. I think one of the biggest challenges of using TDD is learning strategies for isolating the code that’s hard to test and writing code that is easy to test. One of the primary strategies to extend unit test coverage into those hard to reach places is to use mock objects, stubs, or other fake objects i
Jeremy D. Miller -- The Shade Tree Developer wrote Jeremy's Second Law of TDD: Push, Don't Pull
on 03-09-2006 9:59 AM
Laws of TDD is probably something that should be written by someone with more authority in the community,...
Jeremy D. Miller -- The Shade Tree Developer wrote Jeremy's Second Law of TDD: Push, Don't Pull
on 03-09-2006 10:18 AM
Author: <a href="blogs/jeremy.miller">Jeremy D. Miller</a><br />Laws of TDD is probably something that should be written by someone with more authority in the community, but I've got a blog and I thought of it first, so here goes. I'll expound on each of these at some point in the misty future, but just for fun here is Jeremy's Laws of Test Driven Development (I'm certainly not claiming any kind of originality here. Some of these rules predate TDD anyway).
Jeremy D. Miller -- The Shade Tree Developer wrote Jeremy's Second Law of TDD: Push, Don't Pull
on 03-19-2006 7:16 PM
Laws of TDD is probably something that should be written by someone with more authority in the community, but I've got a blog and I thought of it first, so here goes.  I'll expound on each of these at some point in the misty future, but just for fun here
Jeremy D. Miller -- The Shade Tree Developer wrote Achieve Better Results by following Jeremy's Third Law of TDD: Test Small Before Testing Big
on 05-30-2006 2:43 PM
Getting back on track with TDD content.  One of the most important lessons learned the software...
JayFlowers > Unit Testing and TDD Resources wrote JayFlowers > Unit Testing and TDD Resources
on 06-27-2006 9:46 PM
Jeremy D. Miller -- The Shade Tree Developer wrote Best of the Shade Tree Developer (with actual links!)
on 08-07-2006 4:51 PM
Between being extremely short handed at work, tech' reviewing a new book, a
possible book proposal...
Jeremy D. Miller -- The Shade Tree Developer wrote Best of the Shade Tree Developer (with actual links!)
on 09-01-2006 2:33 PM

Between being extremely short handed at work, tech' reviewing a new book, a possible book proposal

Jeremy D. Miller -- The Shade Tree Developer wrote Jay's TDD QuickStart, and the underlying problems he stumbled into
on 03-07-2007 2:05 PM

Jay Kimble , CodeBetter's resident AJAX guru, issued a little challenge to us TDD bloggers about using

Dim Blog As New ThoughtStream(me) wrote Excellent List of TDD Starter Blog Posts
on 03-07-2007 5:05 PM

Jeremy Miller has posted quite a response to a question asked by Jay Kimble on unit testing with the...

Ian Goodsell - Dev Emporium wrote Good Articles on Unit Testing, Design, and Test-Driven Development
on 04-19-2007 4:03 PM

Jeremy D. Miller has some really good articles on unit testing, design and TDD. Here are some gems: ...

Getting with it: Test-Driven Development — James Gregory wrote Getting with it: Test-Driven Development — James Gregory
on 07-17-2007 7:40 PM

Pingback from  Getting with it: Test-Driven Development — James Gregory

on 07-22-2007 5:57 PM

I'm awful at returning emails. It was even one of my new year's resolutions to reply to emails

Chad Myers' Blog wrote Starting a new set of projects, what to do?
on 10-29-2007 3:58 PM

Starting a new set of projects, what to do?

Jeremy D. Miller -- The Shade Tree Developer wrote Resources from my DevTeach talks
on 11-29-2007 12:03 PM

To everybody that attended one of my talks at DevTeach this week. All of the materials are now online

Ian Cooper [MVP] wrote TDD and Hard To Test Areas, Part 2
on 09-10-2008 5:02 PM

It's been a while. I have been heads down on a new project (more about that some other time), and

Community Blogs wrote TDD and Hard To Test Areas, Part 2
on 09-10-2008 5:42 PM

It's been a while. I have been heads down on a new project (more about that some other time), and

Mirrored Blogs wrote TDD and Hard To Test Areas, Part 2
on 09-10-2008 6:00 PM

It's been a while. I have been heads down on a new project (more about that some other time), and

TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 09-10-2008 10:15 PM

Pingback from  TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas

TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 09-13-2008 8:55 PM

Pingback from  TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas

TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 09-15-2008 9:04 AM

Pingback from  TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas

TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 09-16-2008 9:09 AM

Pingback from  TDD and Hard To Test Areas, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Jeremy D. Miller -- The Shade Tree Developer wrote Design and Testability
on 12-10-2008 12:25 AM

My most recent Patterns in Practice article entitled Design for Testability is available in this month's

Community Blogs wrote Design and Testability
on 12-10-2008 12:37 AM

My most recent Patterns in Practice article entitled Design for Testability is available in this month's

Ben wrote re: Haacked on TDD and Jeremy's First Rule of TDD
on 01-22-2009 5:39 PM

I just recently started trying to code using TDD and this post was extremely relevant to what I'm experiencing lately.  

You wrote this in 2005 and I'm just now reading this in 2009.  I guess it's better late than never for me!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?