Thursday, July 23, 2009

Test Driven Development: Introduction

Recently another developer and I have had the privilege of working on a green field project - something that we developers don’t always get a chance to do.  It has been fun, exciting, daunting, frustrating – and above all else, educational.

One thing that we wanted to try on this project, which we really haven’t done in the past, is unit testing (crazy, huh?).  And I had read about some idea that a bunch of weirdos came up with called “Test Driven Development” that sounded intriguing.

So we decided what the heck, let’s TDD this puppy and see what happens.  Of course we didn’t really know anything about TDD, or unit testing in general for that matter.  But with everyone raving about its awesomeness we figured it would just come to us in a zen-like moment of clarity while hacking away at our code.  Needless to say we ran into some bumps along the way, and there were times when we were left scratching our heads saying “I still don’t get it.”  But we learned and failed, and failed and learned, and I feel like we’ve come out the other end of the tunnel with a better understanding of the how’s and why’s of TDD.

This is the first in what is going to be a series on my experiences and understanding of the TDD process.  I am by no means an expert – I still consider myself a novice at this.  However I do feel that I have learned a ton in the past 7 or 8 months, and I think it could be helpful to others.  Especially those wondering why TDD, and how to get started.

The Sample
For all of these posts on TDD, we’re going to need a sample.  So using my great genius I came up with this fake requirements spec:

“Print Invoice” Screen Requirements

Create a “Print Invoice” screen.  When a user enters an order number and clicks the print button, the invoice for that order will be printed.

The invoice should contain:

  • Customer Information (customer number, name, shipping address…)
  • Order Details (product and quantity information)
  • Shipping Costs
  • Sales Tax
  • Invoice Total

Calculating Shipping:

  • Standard shipping is $3.00 per item.
  • If the order total exceeds $100 there is a 10% discount on shipping.
  • There is a maximum shipping charge of $30.

Calculating Sales Tax:

  • Sales tax is 7% of the order total.

Now we’re ready to get going.

0 comments: