Test driven development

Roy Smith roy at panix.com
Thu Jan 24 10:07:47 EST 2008


In article 
<7b0188a3-f6f6-483c-8f41-2dd9b9522268 at v67g2000hse.googlegroups.com>,
 ajcppmod at gmail.com wrote:

> So my question is when approaching a project that you want to employ
> test driven development on how and where do you start? And also if
> anyone uses top-down design with TDD I would be interested in how you
> do it (does it involve lots of mock objects/ is the first test you
> write the last one to pass)?

I've been a big fan of TDD for a few years.

I don't always use it.  When working with legacy code, sometimes the pain 
of getting things into a test harness exceeds the effort I'm able or 
willing to put into it right now.  The other times I don't use it is when 
I'm "just doing some simple little thing" (which inevitably grows into 
something bigger than originally anticipated).  In all cases, I often find 
that code I ended up writing is less well documented, less well tested, and 
more buggy.

You've hit the nail on the head with the top-down, bottom-up issue.  TDD 
(at least in my mind) encourages bottom-up design, because it forces you to 
have working code on day one.  This is not always good.  So, the key (as 
you pointed out) to mixing TDD with top-down, is to use mock objects a lot.  
But, this is a good thing; it forces you to write classes which can be 
tested in isolation.  This no only makes for better tested code, but often 
leads to cleaner design.

Now, to drag this thread back to something apropos to Python, the good news 
is that Python makes it easy to work with mock objects.  Often, all you 
need to do is "import myTestingFoo as Foo" and it all just works.  Since 
functions and classes are first-class objects, it's easy to pass them 
around.  Since Python uses duck typing, you don't have to make sure your 
test class inherets from anything in particular.  It all just works.



More information about the Python-list mailing list