WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Unit Testing in Visual Studio 2005 Team System
Pages: 1, 2, 3, 4

Creating a Test

We will now create a test for the length() method to ensure that the result returned by it is what we expected.

To create a test for the length() method, right-click on the method name and select Create Tests… (see Figure 1).

Figure 1
Figure 1. Creating a test for a method

A window will be displayed to allow you to create additional tests for other methods and properties (see Figure 2). The Output project drop-down list box lets you choose to create a test project using either Visual Basic, C#, or C++. Choose Visual Basic and click OK.

Figure 2
Figure 2. Generate unit tests

You will be prompted to give a name to the test project. Use the default TestProject1 and click Create. The TestProject1 will now be added to Solution Explorer (see Figure 3).

Figure 3
Figure 3. The test project in Solution Explorer

The PointTest.vb class will now be displayed in Visual Studio 2005 (see Figure 4). This class will contain the various test methods that you can use to test the Point class.

Thumbnail, click for full-size image.
Figure 4. The PointTest.vb file--click for full-size image.

Within the PointTest.vb file, the lengthTest() method is the method that performs the actual test on the length() method in our Point class:

<TestMethod()> _
Public Sub lengthTest()
    Dim x As Integer 'TODO: Initialize to an appropriate value

    Dim y As Integer 'TODO: Initialize to an appropriate value

    Dim target As Point = New Point(x, y)

    Dim pointOne As Point = Nothing 'TODO: Initialize to an appropriate 
                                    ' value

    Dim expected As Single
    Dim actual As Single

    actual = target.length(pointOne)

    Assert.AreEqual(expected, actual, _
       "UnitTesting.Point.length did not return the expected value.")
    Assert.Inconclusive("Verify the correctness of this test method.")

End Sub

Note that the method has the <TestMethod> attribute prefixing it. Methods with this attribute are known as test methods.

Let's modify the implementation of the lengthTest() method. Basically we will create and initialize two Point objects and then call the length() method of the Point class to calculate the distance between the two points:

<TestMethod()> _
Public Sub lengthTest()
    Dim x As Integer = 3
    Dim y As Integer = 4

    Dim target As Point = New Point(x, y)

    Dim pointOne As Point = New Point(0, 0)

    Dim expected As Single = 5
    Dim actual As Single

    actual = target.length(pointOne)
    Assert.AreEqual(expected, actual, _
       "UnitTesting.Point.length did not return the expected value.")
End Sub

Once the result is returned from the length() method, we will use the AreEqual() method from the Assert class to check the returned value with the expected value. If the expected value does not match the returned result, the error message set in the AreEqual() method will be displayed.

Pages: 1, 2, 3, 4

Next Pagearrow