On May 13, 2006, at 06:03 PM, SB wrote:
I’m still reading other people’s code and going over the books. As I
was doing this a question sprang up in my mind.
Is it wise for total newbies to attempt test driven development
from the start?
I’m not sure this is a simple yes/no kind of question. I’d say there
are three types of “newbies”: those new to programming, at all; those
new to programming in Rails; and those new to the concepts of TDD/
BDD. The closer you are to being the third, the more likely it is
you’ll be able to pick up TDD, especially as it pertains to your
Rails programming.
Basically, if you’re a newbie like me, chances are your tests might
even be broken themselves. Or will attempting to write tests be good
practice for actual coding as well?
From a TDD perspective, you always start with “broken” tests. It is
the process by which you program to make your tests pass, and then
write more tests, that puts you on the TDD path. Obviously, when you
start out you won’t be creating the most relevant tests, but I would
say that any test that actually exercises your code isn’t a bad test.
It may be superfluous, or even redundant, but if it’s testing
functionality/behavior that needs to be in your app, then correcting
that test is helping you with your progress.
Obviously, one of the things holding me back is not knowing where to
start. Would attempting test driven development be a valid approach
or best left to those with a little more experience?
Looking to integrate TDD (or BDD) is one of the ways that you can
become more agile in your development practices and that is
definitely a good thing. However, starting with TDD can be difficult
when you don’t have a pair to program with, another cornerstone of
Agile Development. You don’t expressly need a pair to do good TDD,
but having an experienced pair, especially when you are first
learning, is a great way to get over those initial humps. One of the
things that my TDD instructor always said was, “writing that first
test is always the hardest.”
If you don’t have a pair, but you still want to try to get into TDD,
one of the things that really helped me was the first chapter of
Refactoring, by Martin F. [1]. Fowler has a nice little example
program (written in Java, but easily mappable to Ruby). What I did,
while following along with his intro to refactoring, was to build his
program in Ruby and build a test suite that verified the code in it’s
initial state. Then, while performing the refactoring examples in the
rest of that chapter, I first wrote tests that I knew would fail, so
that I could verify my refactoring steps in each of his examples.
I found this to be a great help, because I already had some tests so
writing more tests, TDD style, was quite simple, and I didn’t really
have to think about the code I was writing because I was following
the examples. But the key thing is that none of his examples included
the needed tests, so that ended up being by focus for the exercises.
It really helped to solidify the value of TDD, which I had only
recently gotten into, thanks to the Canada on Rails workshop by
Steven B. (my instructor ;).
Obviously, I can’t say if this method would be helpful for anyone
else, but I definitely liked the feel of the process. It was almost
like Fowler was my programming pair, with him “writing” the code that
passed my tests. As a slight side note, I was actually doing this as
a way to teach myself rSpec [2] and BDD [3], not specifically
Test::Unit and TDD, but the process would be exactly the same.
On a semi-related topic, can ZenTest be used with the new “integration
testing” in 1.1 ?
This is something that I don’t know. I haven’t done any work with
ZenTest as of yet…
1: http://www.amazon.com/exec/obidos/ASIN/0201485672
2: <rspec.rubyforge.org>
3: <http://blog.daveastels.com/articles/2005/07/05/a-new-look-at-test-
driven-development>
-Brian