TDD Kata – By Example video

Since attending Roy Osherove’s TDD Master Class I have been doing my daily TDD Kata, I will admit I have missed some days but generally I have been keeping up with it and really seeing the benefit.

Here I talked about what a Kata is and how it compares to Kumon Maths and I also took the opportunity to practice my reviewing techniques by looking at the best and reviewing Uncle Bobs prime factors TDD Kata.

I had promised to provide a web cast of the TDD SharePoint session that I did at the Best Practices conference, but I feel now that I need to really improve my keyboard mastery before taking on that one.  Instead I have taken the opportunity to record my current TDD Kata which is based on Roy Osheroves String Calculator.  I have limited the steps I have done in this recording as I think 30 minutes would get a little boring.

String Calculator

  1. Create a simple String calculator with a method int Add(string numbers)
    1. The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1″ or “1,2″
    2. Start with the simplest test case of an empty string and move to 1 and two numbers
    3. Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
    4. Remember to refactor after each passing test
  2. Allow the Add method to handle an unknown amount of numbers
  3. Allow the Add method to handle new lines between numbers (instead of commas).
    1. the following input is ok:  “1\n2,3″  (will equal 6)
    2. the following input is NOT ok:  “1,\n” 
    3. Make sure you only test for correct inputs. there is no need to test for invalid inputs for these katas
  4. Allow the Add method to handle a different delimiter:
    1. to change a delimiter, the beginning of the string will contain a separate line that looks like this:   “//[delimiter]\n[numbers.]” for example “//;\n1;2″ should return three where the default delimiter is ‘;’ .
    2. the first line is optional. all existing scenarios should still be supported

 

The idea behind TDD is to work through one thing at a time,  so in the example we start with 0 numbers (or empty string) and progress through each requirement continuing to adopt the Red, Green, Refactor approach – hopefully this will be clear from the video.

The Kata is not about knowing how to solve this problem, it is about knowing how to do things quickly and also knowing how to approach problems using TDD. 

In this example,  as in my day job, I am using ReSharper to aid refactoring,  NUnit 2.5 for my tests as this supports TestCase and TestDriven.Net to run the tests.

I have setup some live templates for the Test method and common Assert.AreEquals statements and have added a Keyboard shortcut to Re-Run the tests in TestDriven.Net (I use Alt + R).

Hope you enjoy the video, I have left the sound off - if you perfer some nice background music I can edit and add some :)

Would love to hear your thoughts on this.

TDD Kata – Calculator Part 1 by AndrewWoody from Andrew Woodward on Vimeo.

You can download the WMV video to watch locally from the Vimeo site.

This entry was posted in Agile, Development and tagged , , . Bookmark the permalink.
  • sarastephens

    Very good :D Can we have some music :P ! Not sure I woudl use string.length as a space would break it and I'd still cound that as an empty string. Personal preference I guess.

    Nice work :)

  • http://www.21apps.com AndrewWoody

    :) yes, in hindsight music would be good – but afraid I would get wiped by the copyright peeps.

    I may do a version that is double the speed with some voice over talking about what I'm doing?

    And yes, should use string.IsNullOrEmpty but handling Nulls wasn't a requirement :)

  • http://twitter.com/daveh551 Dave Hanna

    Is there a list of VS keyboard shortcuts, preferably searchable by keyword? I have no idea how to do some of the things you were doing without using a mouse right-click.

  • http://www.21apps.com AndrewWoody

    As per tweet reply, go to Tools > Options > Keyboard and check what is assigned to each event. You can also add your own here like I have with Alt+R to rerun the tests (used to be something on a rectangle).

    Also I use ReSharper which makes this so much easier todo. Mouseless coding is the way to go.

  • http://isaac-abraham.spaces.live.com/blog/ Isaac Abraham

    Nice video! Not sure about the use of foreach for the calculating of the multiple numbers – I used .Sum () when doing this… I think that that’s a little nicer.

    BTW – I wrote a quick blog post about some of the nicer keyboard shortcuts in VS that I recently found, might be of use.

    Cheers

  • http://randomcode.net.nz/ Neal Blomfield

    There’s a keyboard jedi program floating around that shows all your keystrokes – would be very useful for this kind of thing so others can see the keystrokes you are using.

  • http://www.temebele.wordpress.com/ T
  • http://redbitbluebit.com/ John Uhri

    Do you have a ZIP of the solution you could include for us to refer to?

  • Pingback: Roy Osherove’s TDD Kata: My first attempt at Mark Needham

  • Pingback: Markus Tamm » Blog Archive » 25.02.2010

  • Scott Radden

    Are you running NUnit tests within VS IDE?? If so how do you enable this because running VS2008 and NUnit 2.5.3 the and decorating my test fixture with NUnits [TestFixture] attribute the test option in the IDE remains disabled?

  • http://www.21apps.com AndrewWoody

    Scott,

    I use TestDriven.Net and I have mapped the Alt+T and Alt+R keys to this. It can then run the NUnit tests in Visual Studio. The out of the box Unit Testing framework will only work with MSTest test as you have found.
    Andrew

  • http://twitter.com/timhardy Tim Hardy

    Excellent video, but didn't you skip requirement 3 entirely? I believe your code only allows for a single delimiter character within a numberstring instead of allowing different delimiters within the same string. I only see that because I've started doing this kata recently and step 3 adds significant complexity (the Split method doesn't help anymore – unless I'm missing something). I've had to go old-school with the string manipulation and add tests for multiple digit numbers.

  • Pingback: More Cloud News « Composite Code

  • ajwaka

    Thanks for sharing – I don't know why but sometimes I *think* things need to be difficult or confusing. After watching this I realize I spend too much time forward thinking and fail to achieve the “make it pass as EASILY as can be” – which in hindsight has helped to falter my rhythm and lead to abandoning TDD.

    Thanks for the smack on the head!