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 3

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.

Working with the Generated Code
In addition to the ProjectLibrary_Test.vb file, which contains the unit test boilerplate code, you'll find several other files.

  • AuthoringTests.txt contains instructions on how to use the unit testing features of Visual Studio.
  • localtestrun.testrunconfig contains configuration information used by the testing framework. Double-clicking this file opens a window where you can modify the configuration settings for the test.
  • DevXLibrary.vsmdi contains details about the tests you can run. Double-clicking this file opens the Test View pane where you can launch tests.
Visual Studio creates one stub test method for each method selected in the Create Unit Tests dialog. Each stub code test method includes class creation, variables for input parameters and return values, and code to compare the return value to what was expected. If the methods being tested were declared as Shared methods, the code would not include class initialization and would call the method directly. An Assert verifies that the method's return value matches the expected value. If the values do not match, the Assert raises an error that causes the test to fail. Also notice that Visual Studio adds a call to Assert.Inconclusive (with the message "Verify the correctness of this test method.") to the end of all unit test methods it creates. This call alerts the tester that the unit test method has not been implemented. After you have properly implemented the unit test method you should remove this line of code.

Here's the code generated for the SSN_IsValidLength method.

   '''A test for SSN_IsValidLength(ByVal String)
   <TestMethod()> _
   Public Sub SSN_IsValidLength_Test()
       Dim target As ProjectLibrary = New ProjectLibrary
       Dim SSN As String = Nothing 'TODO: Initialize to an appropriate value
       Dim expected As Boolean
       Dim actual As Boolean
       actual = target.SSN_IsValidLength(SSN)
       Assert.AreEqual(expected, actual, 
          "DevXLibrary.ProjectLibrary.SSN_IsValidLength did not return " & _
          "the expected value.")
       Assert.Inconclusive("Verify the correctness of this test method.")
   End Sub
To make this unit test method work correctly, you need to modify the unit test method in several ways. First, set the SSN variable to the value you want this unit test to use for the SSN parameter. Next, set the expected variable to the expected return value of the SSN_IsValidLength when given the SSN parameter. Finally, remove the call to Assert.Inconclusive. The resulting SSN_IsValidLength unit test method should look like this:

   <TestMethod()> _
   <Description("Test for an SSN that is too short.")> _
   Public Sub SSN_IsValidLength_Short_Test()
      Dim target As ProjectLibrary = New ProjectLibrary
      Dim SSN As String = "12345678"
      Dim expected As Boolean = False
      Dim actual As Boolean
      actual = target.SSN_IsValidLength(SSN)
Figure 7. The Test Results Pane: This pane displays the results of the unit tests. To get the description column to display, right click the column headings row, select "Add/Remove Columns," and add the Description field.
Assert.AreEqual(expected, actual, "DevXLibrary.ProjectLibrary.SSN_IsValidLength did " & _ "not return the expected value.") End Sub
If you look closely at the preceding code, you will notice that other changes to the boilerplate code have been made as well. I chose to test the SSN_IsValidLength method by providing a value for SSN with fewer than the required nine characters. To distinguish this test from other tests that might exercise the same method, I gave the test method a distinguishing name: SSN_IsValidLength_Short_Test. I also set an additional attribute on the method named Description. This attribute provides more information about what the method actually tests.

In the downloadable source for this project, I also added two additional tests for the ProjectLibrary's SSN_IsValidLength method named using this naming convention. One tests for SSNs longer than nine characters (SSN_IsValidLength_Long_Test), and the other verifies that the method properly recognizes a valid SSN with nine characters (SSN_IsValidLength_Correct_Test).

To run the tests, click the test project in Solution Explorer and then select "Start Selected Test Project with Debugger" from Visual Studio's Test menu. The Test Results pane opens, displaying the results of the unit tests. Figure 7 shows that all three tests passed, along with the names and descriptions of the test methods. If a unit test fails, the result column displays a red failed message, along with a basic error message describing the problem.

Figure 8. A Failed Test: Here's the Test Results Pane showing a failed test.
Author's Note: The Description column does not appear by default in the Test Results and Test View panes. To display the test descriptions, right click the column heading row, and select "Add/Remove Columns…." Select the Description field and click OK. You will need to perform this same procedure for both the Test View and Test Results panes. Adding the Description column aids greatly in identifying tests and results quickly.

Figure 8 shows the Test Results pane following a failed test.

When you start unit tests as described above, the system runs all the tests in the test project. When you don't want to run all the tests, open the Test View pane by using the Test/Windows/Test View menu item. The Test View pane shows each of the tests contained in the test project, along with each tests' description. To execute specific tests, select the tests you want to run, and click the Run button in the upper left corner.

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