The more I learn, the more I know I don't know. As an MCT, it's my job to continually update my skills. To that end, I've been delving into things that aren't covered in the courses I teach. Things like Unit Testing.
What's A Unit Test?
With Visual Studio 2005 and 2008, you get the ability in some versions like Team System to create a Test Project to do Unit Testing. Before I start talking about creating a unit test, let's review the basics. A unit test verifies that a function actually performs it's duties. Now, there are black box and white box types of tests. A black box test only tests that a function accepts only the datatypes it's designed to accept and returns the data or no data that it's expected to return. In other words, you put in certain stuff and get certain stuff back. Now a white box test actually allows you to know what's going on between the input and output. White box testing ensures that you actually are testing all code pathways. It's called "Code Coverage". For more information about Unit Testing you can check the following links:
It's important to write your Unit Tests before you actually develop your application. After identifying the requirements of the application, a unit test can define the functionality of different pieces of the code before it's written. Unit testing helps to maintain the integrity of your code during refactoring. You'll always be able to test that the function still fulfills it's intended purpose.
Generating a Unit Test in Visual Studio
The simplest method of generating a unit test in Visual Studio is to right click the name of any method and select Create Unit Tests. Be aware that not all versions of Visual Studio support Unit Testing. You can also create a new Test Project to add to the project and then, a test class can be added by either selecting from Add New Test popup or by selecting Unit Test from Add menu. This brings up the Create Unit Tests screen where multiple methods to be tested can be selected.
Visual Studio only actually writes a basic stub for the Unit Test. It's up to you to fill in the blanks for the application. You'll do things like test that the return values are the correct datatype and within the expected value range. You'll also check to make sure the funciton only accepts the data it's suppose to accept.
You'll normally see the following assert statement:
Assert.Inconclusive("A method that does not return a value cannot be verified.");
If you ran the Unit Test for the method you're testing right now, You'd see the error message "Assert.Inconclusive failed. A method that does not return a value cannot be verified." Now, if you comment out the Assert.Inconclusive statement, you'll have a test that passes because the unit test method isn't testing for anything. That's not going to help.
You'll have to write some test code with the Assert statement. You'll be supplying the method with specific values and comparing the results with the expected result in an Assert statement.
Unit Test Design is Obviously Important
Obviously, did I say that again? Yup, I did. You've got to think of all possibilities going into the method and all coming out of the method. This takes some prior planning. Now it might seem backwards based on what I've just said about right clicking a method and/or creating a Test Project to add to your application. The key is to think out your Unit Tests ahead of time so that you can implement them easily using Visual Studio.
Additional Resources
Good Luck Unit Testing
I've written an additional Unit Testing Review with more links @ Unit Testing Review List