HOWTO: Creating Test Units in Visual Studio 2008

By nelsonbarlow

Today I attended a workshop on Visual Studio Team System 2008 at Microsoft’s official headquarter in my hometown Recife, Brazil. One of the key aspects as a developer that I believe I don’t do a lot of is testing the application.

One of the features that Igor Abade showed us was the Test Unit. It immediately became clear to me how this could be applied in my work environment and how it could eventually become a common practice in my company. Important features could have test units associated and prevent a meltdown when another feature is added or modified.

Check out the step-by-step guide after the break! 

Now the first thing to realize about this feature, is that it’s not available with all versions of visual studio. In fact, it is only available for the Team System version which is the more complete. There are several debates and even a petition about whether or not Microsoft should include unit testing in all versions of visual studio. I will let you follow the links and make up your own mind.

In order to further understand the use of unit testing, I will – as I did in my other howto blog post- describe a brief problem and follow with a howto of how I solved the problem using unit testing.

Problem:

The following code illustrates a simple Account class in C# that follow these simple rules to calculate the account’s credit:

  • If the balance of the account is smaller than or equal to $2,000 , the owner of the account will receive an immediate approval of 10% of it’s balance.
  • If the balance is greater than $2,000 , the account’s owner will receive 20% if the balance.
  • Additionally, if the owner has a premium account, he’ll receive $1,000 of extra credit.
A simple C# function to calculate immediate Credit approval for a given account.

A simple C# function to calculate immediate Credit approval for a given account.

To protect this important feature from suffering alterations in the future that’ll keep it from working properly, you can create a test unit. This test unit can later be added to a build sequence that will keep the developer from deploying a version with a serious bug.
Right Click on the Text Editor and select "Create Unit Tests"
Right Click on the Text Editor and select
A wizard will pop up in which you select exactly what you want to test. In the combobox below you can select in what .net language you wish the test to be in.

A wizard will pop up in which you select exactly what you want to test. In the combobox below you can select in what .net language you wish the test to be in.

Select the name of the project.

Select the name of the project.

If you've chosen a shared type, you'll be asked if you want to Add the InternalsVisibleTo attribute. Click yes or you'll get compilation errors.

If you've chosen a shared type, you'll be asked if you want to add the InternalsVisibleTrue attribute. If you don't you'll get compilation errors, so click YES.

Scroll down to the end of the Test file that automatically is set on your VS. Here you'll see a few lines of code marked with TODO. These are the lines of code you'll modify to create the test.

Scroll down to the end of the Test file that automatically is set on your VS. Here you'll see a few lines of code marked with TODO. These are the lines of code you'll modify to create the test.

Modify the value of the parameters with the desired input, and set the desired output in the expected variable.

Modify the value of the parameters with the desired input, and set the desired output in the expected variable.

In my case, I have commented the last line of code. It can be use if you want to test for an inconclusive situation. In our case, we're going to pretend it's conclusive.

In my case, I have commented the last line of code. It can be use if you want to test for an inconclusive situation. In our case, we're going to pretend it's conclusive.

Last but not least, run the test and check if it passes. Try modifying the code and see what happens.

Last but not least, run the test and check if it passes. Try modifying the code and see what happens.

It’s as simple as that my friends. Feel free to leave a comment. In a later post I’ll talk about code coverage which is also a very important aspect of certifying the quality of the code and how visual studio helps you keep it well covered.

Tags: , ,

One Response to “HOWTO: Creating Test Units in Visual Studio 2008”

  1. Peter Provost Says:

    Good introduction Nelson. One thing about that petition you link to is that at the time it was posted, Unit Testing features were only available in VSTS Test Edition and Suite. What the petition was calling for has happened and now Unit Testing is available all the way down to the Pro version of Visual Studio.

Leave a Reply