Test driven development (TDD) in VSTS 2010

The last 2 days I have been testing a little piece of VSTS 2010. Something I liked is the way VSTS 2010 supports TDD. A lot of developers are familiar with TDD, some hate it and some love it. I don’t have a lot of experience with TDD, but I am very interested and tried this way of programming on my last project. Test-First development is something which makes you look different to your own code. I realized that I was focused even more on the signature of the method that I was writing. What are good input parameters and what are my return values.

When you start writing your first Unit test you will run into the annoying thing that every class you instantiate isn’t available yet. Well in VSTS 2010 the class is still not available, but there is a very simple way to make them available without losing the focus on your Unit test. See the screenshot below.

Generate class 

In this screenshot you see that the class which is giving an error because it doesn’t exist has a new option to create the class for me. You have 2 options:

  • Create the class directly in the project where the Unit test resides
  • Create the class in another project of your own choice

I think the second option is the best option, because you don’t want all your classes in the same project with all your test files. When you click on the section option (Generate other…) you will get the following panel.

Enter class information

As you can see in this panel you are able to select the following things:

  • Access modifier (Default, Public, Private, Internal)
  • Type (Class, Struct, Interface, Enum)
  • Project location (The project where you want the class to be created)
  • File name: (The name of the file)

Something what is missing is the opportunity to enter a namespace where the class should belong to. Default the class will belong to the same namespace where the unit test belongs to. Hopefully Microsoft will fix this before the release VSTS 2010. I changed the project location to another project because I don’t want my classes inside my test project. When you create the class in another project VSTS will automatically add a reference to the project where the class will be created.

Solution Explorer

Well the unit test isn’t finished yet. In my Unit test I want to test a method which multiplies the incoming parameter. So I need a method with the following signature:

  • Name: Multiply
  • Input parameter: int
  • Return parameter: int

I write this method down in my Unit test in a way it should be invoked if it exists.

New method

You can stub this method without leaving you Unit test. When VSTS creates a stub for me, it will implement the method with throwing a NotImplementedException. This is fine for me, because it doesn’t matter if the first run of my Unit test will fail. At the end the unit test will look like this.

Unit Test

The class which is generated for me will look like this.

Generated class

As you can see in the screenshot, the Multipy method is created with the right signature and it will throw an exception. The namespace isn’t correct like I already mentioned. The next thing to do is write functional code which will make the unit test pass. So test first development is something which is simple to do in VSTS 2010. When VSTS 2010 is released we are able to write our code in combination with unit tests in two ways:

  • Write our functional code first and generate a Unit test based on the class
  • Write a unit test and generate a class which supports those functionalities you are looking for

When this version of VSTS is released i am happy to do some TDD.

Advertisements

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: