RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Improve Code Quality with Unit Testing in Visual Studio Team Edition : Page 2

Learn to use Visual Studio Team Edition's built-in unit test generation and code coverage analysis capabilities to ensure all your code gets tested.

Build Your First Unit Test
The ProjectLibrary class in the sample project defines three simple methods.

  • SSN_IsValidLength simply checks the length of an SSN parameter to ensure that the string contains nine characters.
  • SSN_IsValid is a more complex verification of the SSN against a set of rules for SSNs.
  • SSN_StateOfIssue uses the SSN to find the state where it was likely issued, using the first 3 digits of the SSN to find a matching state. This method could have been coded against a database, but coding it as a large select case made it a great example for this exercise. Further, rather than using Shared methods to make interaction with the class simpler, I chose to use instance methods to demonstrate another feature of the unit testing framework.
Here's the code for SSN_IsValidLength.

Figure 2. Creating Unit Test Boilerplate Code: In Visual Studio, right click the name of the method you wish to test and then select "Create Unit Tests."
   ' Verifies that an SSN is the valid length
   Public Function SSN_IsValidLength(ByVal SSN As String) _
      As Boolean
      Dim blnReturn As Boolean
      If String.IsNullOrEmpty(SSN) Then
         ' an SSN must be provided
         blnReturn = False
      ElseIf SSN.Length <> 9 Then
         ' an SSN must be 9 numbers long
         blnReturn = False
         ' the length must be correct
         blnReturn = True
      End If
      Return blnReturn
   End Function
To create a unit test for this method, right click the method name and select "Create Unit Tests…," as shown in Figure 2.

Figure 3. The Create Unit Tests Dialog: Using this dialog, you can select the methods for which you want to create unit tests automatically.
Figure 4. The Test Generation Settings Dialog: This dialog provides control over test project creation and boilerplate test code generation.
Figure 5. Specify Output Language: The Create Unit Tests dialog allows you to specify whether the unit tests should be generated using C# or Visual Basic.
Figure 6. Generated Unit Test Project: The system generates the project and adds it to the Solution Explorer.
Figure 3 shows the Create Unit Tests dialog and all the methods available in the ProjectLibrary class. Select the checkbox for each method for which you wish to create unit tests (all three methods in this example).

Click the "Settings…" button to open the "Test Generation Settings" dialog as shown in Figure 4.

This dialog provides several options that determine how you want the system to write the test class and boilerplate unit test code. I have modified the default settings only to add an underscore character before the word "Test" in the file name, class name, and method names of the resulting code. As shown in Figure 5, the Create Unit Tests dialog also allows you to specify whether to generate unit tests in C# or Visual Basic.

After clicking "OK," you will be prompted for the name of the new unit testing project. When you click "Create" in this dialog, Visual Studio creates the unit test project, and adds the resulting project to the Solution Explorer as shown in Figure 6.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date