Thoughts on Technology, Methodology and Programming.

Some invaluable TDD tidbits and guidelines

Posted by Marcus Wyatt on 10 September 2005

A Good habit when you are doing TDD is to adopt the practice of making a Test List before you start your coding session.

Here are some guidelines that helped me in creating the lists:

  1. List every operation you know you’ll need to implement
  2. For the operations that don’t already exist, write the null version test of that operation down.
  3. List all the refactorings you think you’ll have to do in order to have clean code at the end of the session.
  4. The implementation will reveal new test and refactorings, write them down on the list as soon as you think about them.

When we look at how to write test the following where of great help to guide me through the process:

  1. Write the Assert that will pass when the test is done first:
    public void Should_Open_File_Read_First_Line()
       Assert.AreEqual("Line 1", reader.ReadLine() ); 
  2. Now you should look at the object or variable used in the Assert and solve where that functionality comes from:
    public void Should_Open_File_Read_First_Line()
       StreamReader reader = new StreamReader(fileSteam); 
       Assert.AreEqual("Line 1", reader.ReadLine() ); 
  3. Now we need to solve the following problem, where do we get the fileStream object from:
    [ Test ]
    public void Should_Open_File_Read_First_Line()
       string filePath = @"C:/test.txt";
       FileStream fileStream = File.Open( filePath, FileMode.Open );
       StreamReader reader = new StreamReader( fileStream );
       Assert.AreEqual( "Line 1", reader.ReadLine() );

This method of writing test during a TDD session has helped me to write better test. James Newkirk first described the technique of Assert-first tests and it has a powerful simplifying effect. Here are some questions you can ask when you use this Assert-first principle:

  • Where does the functionality belong?
  • What should the names be called?
  • How are you going to check for the right answer?
  • What is the right answer?
  • What other tests does this test suggest?

These guidelines have helped me become a better TDD developer. Hope they will help you just as much. I can really recommend James Newkirk‘s book “Test-Driven Development in Microsoft.NET” as well as Kent Beck’s “Test-Driven Development by Example“. Both these books are great resources on TDD, Kent’s book explores TDD and the Patterns and practices around TDD and James book covers doing TDD in the .NET environment in detail. They are both excellent reads!

tags: , , ,

Currently listening to: *** 566. diminish – feel that [out of grace mix] – Winamp ***


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: