Test driven development in Visual Studio 2010

, posted: 1-Dec-2011 14:33

For every development project it is definitely a great idea to incorporate some sort of testing, either automated or by user. In this blog post I will be covering some of the basics with automated testing using Visual Studio 2010, more specifically Unit Testing.

Within the IDE you will need to create a Test Project, which you be able to easily find under your chosen language -> project types.



This will create another project in your solution that will be just for your Unit Tests. In this solution, right click and select Add Item -> New Test, which will open the next dialog:



From in this New Test dialog, select Unit Test Wizard to bring up the wizard screen for selecting the classes you wish to automatically generate Test Classes for:



When selecting the class you wish to generate a test for, you can select what methods/properties of the class you want to test if you want to go down to that level. For my testing, I select the Class level, to test all the methods/properties included in the class. Once you have selected your classes Visual Studio will create individual classes for each Class you are testing. In this example I have selected the Password Library class:



There are two test functions here. Visual Studio only generated one function for testing IsNumber(), but I made a duplicate copy to test two different values (one being a number, the other being a letter).

To understand what it's doing inside the test function, you start by defining what you expect as the result, and then what conditions to set then calling them. Once you have done that, you compare what you got with what you expected - by using the Assert object (part of the Visual Studio Test Units framework) along with a specific function you need - in this case I am using AreEqual() - which will return a Pass if both results are the same (Actual vs Expected), else will result in a Fail if they aren't.

Once you have setup your tests you need to right-click on your Test Project, and select as a Start-up Project. Then Build/Run your solution - you will then get this nice window at the bottom of your Visual Studio instance:



So here we can see there were six auto-generated tests by the Test Wizard, with the addition of the IsNumberTest test function that we duplicated (so seven in total). We can easily see that five of the seven have passed successfully.

So that is the basics of creating, modifying and running Unit Tests as part of the Visual Studio Test Units framework. It is all built into Visual Studio, so there is no additional add-ins or downloads to have this nice functionality.

The last bit of advice I can post - run these tests as often as you modify classes. If you do modify your classes often, then it is best to re-run or modify then re-run these to have a sanity check on your code to make-sure it all runs as expected.

Download a trial of Visual Studio 2010.

About the author

Stephen Aitchison is senior developer at Aura Redeye Security Ltd. You can find him on Twitter as @NZCoderGuy and on Geekzone as well.





Add a comment

Please note: comments that are inappropriate or promotional in nature will be deleted. E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.

Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments. If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and shown in this blog post.

Your name:

Your e-mail:

Your webpage:

About the Visual Studio blog

In the years since the hugely successful release of Visual Studio 2005, Microsoft has used developer feedback from all over the world to introduce new features in later releases.

This sponsored blog will bring Visual Studio tips and tricks from well known developers in the New Zealand tech community directly to you.

Every second day during November and December 2011 you will find something new here. Make sure you bookmark this blog or subscribe to its RSS feed.    

Join us on Twitter

Find us on Facebook

Tags

Blog...
Database...
Techniques...
Testing...
Tools...
User Interface...
Windows Phone...


Other blog posts

Winners of Windows Phone compe...
Customise your Visual Studio 2...
Secure Development Tips and Te...
NuGet Package Manager extensio...
Competition: be in to win one ...
Consuming JSON web APIs with V...
Recommended documentation add-...
Web.config transforms...
Getting Started with Visual St...
Welcome to the Visual Studio b...


Recent comments

kiwiandy on Winners of Windows Phone competition announced, mo: mm the first one I try... ASB.. isnt there!?...

chiefie on Winners of Windows Phone competition announced, mo: Hmm the ASB Bank app is MIA....

Daniel Ballinger on Recommended documentation add-on for Visual Studio: Personally I don't find much value from a tool like GhostDoc if the generated do...



Disclaimer

The Visual Studio blog is sponsored by Microsoft NZ. The blog posts are the authors' genuine accounts of their experiences with Visual Studio, Windows Phone SDK and Windows Azure Platform and are not influenced or filtered by Microsoft New Zealand in any way.