In the Create Unit Tests dialog, select any other additional members you want to test and click OK (see Figure 2-70).
Figure 2-70
You are prompted to name the test project. Use the default TestProject1
and click Create. You may also be prompted with the dialog shown in Figure 2-71. Click Yes.
Figure 2-71
The TestProject1
is be added to Solution Explorer (see Figure 2-72).
Figure 2-72
The content of the PointTest.cs
class is now displayed in Visual Studio 2008. This class contains the various methods that you can use to test the Point
class. In particular, note the lengthTest()
method:
/// < summary>
///A test for length
///
[TestMethod()]
public void lengthTest() {
Point target = new Point(); // TODO: Initialize to an appropriate value
Point pointOne = null; // TODO: Initialize to an appropriate value
double expected = 0F; // TODO: Initialize to an appropriate value
double actual;
actual = target.length(pointOne);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
The lengthTest()
method has the [TestMethod]
attribute prefixing it. Methods with that attribute are known as test methods.
Now modify the implementation of the lengthTest()
method to basically create and initialize two Point
objects and then call the length()
method of the Point
class to calculate the distance between the two points:
///
///A test for length
///
[TestMethod()]
public void lengthTest() {
int x = 3;
int y = 4;
Point target = new Point(x, y);
Point pointOne = new Point(0,0);
double expected = 5F;
double actual;
actual = target.length(pointOne);
Assert.AreEqual(expected, actual,
"UnitTesting.Point.length did not return the expected value.");
}
Once the result is returned from the length()
method, you use the AreEqual()
method from the Assert
class to check the returned value against the expected value. If the expected value does not match the returned result, the error message set in the AreEqual()
method is displayed.
Before you run the unit test, take a look at the Test Tools toolbar (see Figure 2-73) automatically shown in Visual Studio 2008.
Figure 2-73
To run the unit test, click the Run All Tests in Solution button in the toolbar. In this case, the lengthTest()
method passed the test. The length between two points (3,4) and (0,0) is indeed 5 (see Figure 2-74).
Figure 2-74
You can make modifications to the lengthTest() method to test other parameters. In the Test Results window, you have the option to view the previous test results (see Figure 2-75).
Figure 2-75
Testing with Floating Point Numbers
You need to take special note when your test involves comparing floating point numbers. Consider the following example:
[TestMethod()]
public void lengthTest() {
int x = 4;
int y = 5;
Point target = new Point(x, y);
Point pointOne = new Point(1,2);
double expected = 4.24264F;
double actual;
actual = target.length(pointOne);
Assert.AreEqual(expected, actual,
"UnitTesting.Point.length did not return the expected value.");
}
When you run the test, the test will fail (see Figure 2-76).
Figure 2-76
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.2426400184631348, and the result returned by the length()
method is actually 4.2426406871192848. The AreEqual()
method actually fails if you compare them directly.
To address this issue, the AreEqual()
method supports a third parameter — delta
— that specifies the maximum difference allowed for the two numbers that you are comparing. In this case, the difference between the two numbers is 0.0000066865615. And so the following code will pass the test:
Assert.AreEqual(expected, actual, 0.0000066865616,
"UnitTesting.Point.length did not return the expected value.");
But this code will fail:
Assert.AreEqual(expected, actual, 0.0000066865615,
"UnitTesting.Point.length did not return the expected value.");
Assert.AreEqual(expected, actual, 0.0000066865614,
"UnitTesting.Point.length did not return the expected value.");
Although the documentation says that the delta specifies the maximum difference allowed for the two numbers, in actual testing the difference should be less than the delta for the Assert.AreEqual()
method to pass. This explains why that first statement fails.
Adding Additional Test Methods
You can insert additional test methods by adding new subroutines to the PointTest.cs
file and prefixing them with the [TestMethod]
attribute. For example, the following test method uses the AreSame()
method of the Assert
class to check whether two objects are pointing to the same reference:
[TestMethod()]
public void objectTest() {
Point point1 = new Point(4, 5);
Point point2 = new Point() { x = 4, y = 5 };
Point point3 = point2;
//---Failed---
Assert.AreSame(point1, point2, "point1 is not the same as point2";
//---Passed---
Assert.AreSame(point2, point3, "point2 is not the same as point3";
}
Figure 2-77 shows the test results.
Figure 2-77
Читать дальше