VS 2005 Test-Driven Development?

Test-Driven SoftwareUnfortunately Visual Studio 2005 RTM isn’t online, as Jeffrey Palermo ‘promised‘. Strange enough his post has been removed from his blog.

In his last post, he talks about what MSDN says about Test-Driven Development. Microsoft advises people to draw classes and interfaces with the class designer. As we know, the code is then automatically added to your projects. Then Microsoft advises to generate test classes from your designed classes and interfaces, and then the TDD process begins.

Jeffrey says, Microsoft isn’t creating the tool to fit the process, but redefines the process to fit the tool, as it seems. This is indeed a bit strange, especially when you believe one of the main reasons to use Test-Driven Development is so you’ll have a better (loosely coupled) design. When you first design all your classes and interfaces, and generate tests from these, it’s totally the other way around again. But I’m a little confused by what Microsoft says in their guideline here.

I’m currently preparing a presentation for a meeting within our company, to explain Test-Driven development to my colleagues. I’m also using Visual Studio 2005 and I’m loving the refactoring support in it. I (unfortunately) haven’t used Resharper a lot in the past, but Jeffrey says VS2005 won’t generate production method stubs from a test, while Resharper can do this in VS2003. I however have used this and it works (almost1) great. What I did is create two projects, one for my implementation classes and one for my tests. I’ve just drawn (dragged ‘n’ dropped) some classes in the designer, which were than generated into my project. I did not create any properties or methods, just the empty classes. Then I added a reference from my test-project to my implementation-project. I added a TestMyImplementationClass testclass (confusing yet?) and wrote a test method. VS2005 immediately began to make noise that it could not find the methods I was referring to. This is correct, because I had not written any code inside my newly designed classes. I hovered over the method I tried to call, and this little refactoring icon appeared. I hit the "Create method stub" option and the method appeared in my implementation class, with a beautiful NotImplementedException inside the method. That’s it! It hit the execute tests button and I got a red light, and no compile errors.

I then implemented the code, ran the test again and the test succeeded, green light!

Isn’t this exactly what Microsoft is saying in their guideline? I am however a bit confused by the fact that even Microsoft states: 

The traditional TDD process does not contain this step. Instead, it advises that you create tests first. This step is included here so that, while creating tests, you can take advantage of two features in Visual Studio 2005 Team System: the GUI design capabilities of the Class Designer, and the automatic test-generation capabilities of Team Edition for Testers.

  As Uncle Bob says, in Agile/XP we don’t do BDUF, but we definitely do think about design and/or specifications. And the Microsoft guideline doesn’t tell you to write a lot of code (or do BDUF), but to add a minimum of code, just enough to compile. Isn’t that what we’re supposed to do, just write the minimum amount of code so we don’t get compilation errors? We have to be able to run the tests, or else we don’t get the red light we need.

The Microsoft guideline is a little vague, but in my opinion not (totally) wrong.

1 Almost great, because VS2005 sometimes gets a little bit confused. In one test, I called two methods that weren’t there yet. The first I could generate via refactoring, the other I couldn’t, because the option just didn’t came up. After screwing around, it did however.

[update] : I got one bit wrong, and that was that you indeed cannot draw a lot of classes and then start your Test-Driven Development, even less while generating tests for you from the IDE. I still think the designer can come in handy though, when you’re more ‘drawing’ out some options, playing with patterns perhaps, etc. Test-Driven Development however is about design, and when you first design something and then do TDD, you got it all wrong. 🙂

You may also like...

3 Responses

  1. Frans Bouma says:

    It seems the Agile/XP/TDD movement cooks up acronyms / names by the minute. WTF is ‘BDUF’ ? 🙂

    All this bitching about VSTS not doing TDD… I really wonder how these people think you should TDD with a team of say 500 people, you know the target audience of VSTS.

  2. Yep, the 15th came and went with no Whidbey. Now the date floating around is Oct 27th, but I’ve given up counting on dates. It comes when it comes.

    Great info on method generation. I did a lot of exploration with the Beta1, and it couldn’t do method generation from tests, and when I went to Tech Ed, I specifically asked a few speakers about this. They always demoed generating tests from production code, and when asked if it worked the other way around, they said “no”, so I haven’t tried to prove them wrong. I’m glad this capability does, in fact, exist because I’ll need it.

    In the Microsoft guidelines for TDD, steps 5 and 6 are:

    5: Define the interfaces and classes for your feature or requirement. You can add a minimum of code, just enough to compile. Consider using the Class Designer to follow this step. For more information, see Designing Classes and Types.

    6. Generate tests from your interfaces and classes. For more information, see How to: Generate a Unit Test.

    This is the part that is backwards from what TDD really is. With this method, you have already designed your class and then generate tests from it. That’s the part I have a problem with. Their step should be:
    1. Write a test. Generate a method from it. Red, Green, Refactor. Rinse, Repeat.

    Other than that, I’m fine with it.

    I like Uncle Bob’s book as well. I learned Agile from that book. Great book.

    Thanks for keeping me in line. 🙂

  3. @Frans : BDUF = Big Design Up Front, something you’ve probably heard of. Otherwise, look it up in Ward Cunningham’s wiki or Wikipedia.

    Besides that, it’s perhaps also confusion about VS2005 and VSTS. I’m not sure what VSTS is, but I know what VS2005 and what VS Team Foundation (Server) is. You don’t need Team Foundation to use TDD and develop applications. So no need for a target audience for 500 people! 😉
    And yes, there have been projects done with XP and such with a large number of people, be it developers or other specialists. They were just divided by smaller teams. I’ve never ever come across teams of 500, and don’t think I’ll ever will. The largest one was 50 people, and on those were 15 developers or so. They’d be able to do TDD for sure.

    @Jeffrey : Generating tests from your classes is indeed wrong, you’re right about that. What I did, is having no code in my classes, just enough so I could generate the method from my test.

    That’s working out GREAT!

Leave a Reply

Your email address will not be published. Required fields are marked *