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

Darrell Norton's Blog [MVP]

Fill in description here...

Notes from Test Driven Development: By Example (Kent Beck)

I have to admit that as much as I have done Test Driven Development, I have not read Kent Beck’s book all the way through. It certainly has not hindered me, but since I have the book to give away at an upcoming WeProgram.NET meeting on TDD, I figured I’d breeze through it in a couple of hours. Here are my notes, in case anyone finds them useful.

Notes from Test Driven Development: By Example, Kent Beck

  • There are three methods of going forward when doing TDD: fake it, use an obvious implementation, and triangulation.
    • Fake it is where you just return the exact value you need. If your test expects a zero from a method, use a “return 0;” statement. Usually you use this when you cannot tell how to implement certain functionality, or your previous steps were too large and you cannot figure out what went wrong. Something that works is better than something that doesn’t work!
    • Use obvious implementation when you are pretty sure of the code you need to write, so write it and see if the test passes. The majority of the time you will use this method to move forward with TDD quickly.
    • Triangulation is where you want to generalize a certain behavior, but are not sure how to do it. So you start with fake it and add additional tests that force the code to be more generic along a certain dimension. This is how you test the code’s axes of variance (measure some code’s resilience to change along a specified dimension). For example, to test the money code’s axis of variance for different currencies, Kent added tests for Francs in addition to Dollars.
  • If you inherit code without unit tests, retroactively setup unit tests for the section of code you are working to make sure you don’t introduce any new bugs with your bug fix. Do not attempt to setup unit tests for the entire application.
  • Test Driven Development keeps the developer working at the fastest speed possible for the current conditions. Is the step size feeling too restrictive? Use obvious implementation to take bigger steps and accomplish more with each run of the unit tests suite. Are you getting surprised by red bars? Slow down and try faking it or triangulation and run the test suite more often.
  • Keep a test list. This list should contain three categories of items: new code, refactorings, and new tests. For example, if you think about an extension point (new code) for the code you are working on, write it down but keep working on what you are doing at this instant. Focus is the key.
  • A common mistake is to write tests that pass after the code is written. Don’t do this. Although Kent gives one, rather philosophical reason, my reason is that if it hasn’t failed, you haven’t proved the test works. The test should fail, you should add or change some code, and then the test should pass. This (almost) ensures that the code works and it works the way you think it works.


Comments

Darrell said:

Jiho - TDD is always useful, in my opinion. When doing ASP.NET development, I can use TDD to drive my data access and business layers to make sure they work before even starting the UI. There is also a product called NUnitAsp that allows you to easily develop ASP.NET web sites using TDD principles and NUnit. For distributed apps using web services, an important thing that would help is mock objects. Look for the DotNetMock open source software.

I'm not sure about custom server controls since I've never written any, though other people on my team have (not sure if they used TDD at the time or not, it's been a while).

Maybe someone else can elaborate?
# May 10, 2004 6:59 AM

Jiho Han said:

How much time would TDD based development add to non-TDD based approach? I am basically looking for a away to sell this approach to management in a way that the management can understand.
# May 10, 2004 9:38 AM

Darrell said:

Jiho - let me see what I can come up with. I'll post about it shortly (in a day or two).
# May 10, 2004 10:02 AM

keith ray said:

Jiho, TDD eliminates about 99% of the time time you spend in the debugger, so doing software development using TDD is probably faster than writing code without unit tests, and *definately* faster than writing tests after writing the code to be tested.

(And don't forget that refactoring in small increments is part of doing TDD.)
# May 10, 2004 4:03 PM
Check out Devlicio.us!

Our Sponsors