Running a Test

Once you have created your tests, you are ready to run them. Select Test O Run O All Tests in Solution to run all of the tests. Alternately, you could select to run an individual test (Test O Run O Tests in Current

Context or from the Test View window). This will compile your solution and execute the tests. With a bit of luck, you should see the icon beside each test switch from Pending, through In Progress, to Passed in the Test Results window (see Figure 7-7).

TeU Remit}

liJi 31

[email protected]!N 3(110-01-? -

Run ' fej» Debug * U J || isF * % 3

j ¿) Test run comoleted Results: 4/4 cassed' Itemfs!* checked: 0


T«t Namp

Projprt FrrnrMptugr

□ Passed

Avera g eOfZeros EsZero


□ ♦¿J^ Passed

Averag eOf KnownRa nge


□ Passed

Ave rag eOf NottimglsNa N


—1 Passed

Ave rag eOf Max Value slslnf inity


To see a failing test, make a change to the Average method that might reflect a simple, but common logic error:

Public Class Stats

Public Function Average(ByVal ParamArray values() As Double) As Double Dim result As Double Dim sum As Double

For i As Integer = 1 To values.Count - 1

Next result = sum / values.Count Return result End Function End Class

Code snippet from StatsTest

This is a common error: You may forget when a collection is zero-based or one-based. If you build the solution and run all the tests, you will see that two succeed while two fail (see Figure 7-8).

Tr\l Results

B | Q Jj v-trnt■■Q'.'-l.'Fm'.-UaiN MliU>1 J . Run ' Kj Debug - ! dj -i? * *J 3

o lEiLLUnMlEil Results: 2/4 passes: ltemts;d>edted: 2

Z ''' - I d AverageOtWothNnglsWaN F do. Math .Tests

Patsed AverageOTZerasIsZerc Fdo. Math .Tests

^ Failed Aveiag e Otttn ownRa n ge Fdo. Math Tests Assert-AreEqual tailed. Expede £t: f S.J ^. Adualr-f5.Average of L-10 is not 5.5

t_JQ Failed Aveiag e Of MaxValueili Infinity Fa o. Math .Tests Assert.AreEqual iai le d. Expede d: 5,938i&5674311 S3 E -307s-. Aduab <TnFin rty *

figure 7-8

The AverageOfKnownRange test is failing because you are no longer adding up all the provided numbers. The error message returned also helps identify this problem. Notice that it includes the message you added to the test method:

Assert.AreEqual failed. Expected:<5.4>. Actual:<5.5>. Average of 1-10 is not 5.5

Similarly, the AverageOfMaxValueslslnfinity test fails because you are only adding the first Double.MaxValue to the calculation, so no overflow occurs.

Change the code back to use the appropriate index and click the Run button in the Test Results window. By default, it will run only the checked tests. In this case, that means the two failing tests. You should now see the two tests pass.

0 0

Post a comment