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.