Unit Testing your Code using Visual Studio 2010

Posted: May 9, 2011 in Techie Stuffs

Unit Testing is a big Question now a days when we talk about the latest technology, but can we really rely on manual unit testing a very complex piece of code ? I guess no ! so what to do ? here comes in picture the new Visual Studio 2010

Lets take an example that we have to build a piece of Code that would Add two integers, and following is the code:

Once you are done coding your method/function right click on your Method and Choose “Create Unit Tests…” as mentioned in the below screen shot

After selecting “Create Unit Tests…” from the Context menu you will get a screen for Creating Unit Test, chose your Method/Function name and hit OK button give your Test Project name and Continue to the Test Class

Once your Test Project is created, Visual Studio will now automatically open  the Test Class with a Method attibuted as “[TestMethod()]” as given below and method you selected will be present inside this Test method. Provide the parameter values and continue to run your project by making this Test Project as start up project:

In our example we provided lFirstNumber as 10, lSecondNumber as 20 and expected as 30 which is nothing be the output of “AddTwoNumbers” method. After running we will get the following result which is nothing but the passed Unit Test:

In case we had given wrong result the Unit test would have failed :

Hope this post will help you create your Unit test and build better applications , thanks

Advertisements
Comments
  1. Techie says:

    nice…

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