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

Running the Test

To run the test, first set the TestProject1 as the startup project (right-click on TestProject1 in Solution Explorer and select Set as Startup Project).

Press F5 and observe the results shown in the Test Results window (see Figure 5).

Figure 5
Figure 5. Running the test

In this case, the lengthTest() method passed the test. The length between two points (3,4) and (0,0) is indeed 5.

You can make modifications to the lengthTest() method to test other additional parameters. In the Test Results window, you have the option to view the previous test results (see Figure 6).

Figure 6
Figure 6. Viewing the previous test results

You need to take special note when your test involves comparing floating point numbers. Consider the following example:

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

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

    Dim pointOne As Point = New Point(1, 2)

    Dim expected As Single = 4.24264
    Dim actual As Single

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

If you were to manually calculate the length between the two points (4,5) and (1,2), you would indeed obtain the correct result as expected: 4.24264. However, when you run the test, you would notice that the test failed (see Figure 7).

Figure 7
Figure 7. The lengthTest method failed

Why is this so? The reason is that floating point numbers (such as Single and Double) are not stored exactly as what they have been assigned. For example, in this case, the value of 4.24264 is stored internally as 4.24264001846313, and the result returned by the length() method is actually 4.24264049530029. Hence the AreEqual() method will actually fail if you compare them directly.

Pages: 1, 2, 3, 4

Next Pagearrow