按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
the answers returned by the method with targeted responses。 The If statement is used to check
whether the value of the variable total is equal to 3。
When writing test code; the way the Add() method is used must be the same way the
console or Windows application uses the method。 Otherwise; it would be like testing a winter
tire in the middle of the Sahara—fun to do but irrelevant。
Another question related to testing has to do with the timing of tests。 Do you create the
tests before or after implementing the Add() method? To get a clear understanding of the problem;
imagine the development of a tire。 Do you define the tests for the tire before or after the tire has
been developed? Most likely; the answer is before; during; and after development。 This is an
important consideration when developing software。 Tests are written before; during; and after
implementation; as follows:
…………………………………………………………Page 58……………………………………………………………
36 CH AP T E R 2 ■ L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S
o You develop tests before implementing the Add() method to get an idea of what name
spaces; classes; and methods you will be defining。 The definition of the different items
gives the developer an idea of how the items will be used。
o You develop tests during the implementation of the Add() method to verify that your
source code implementation is on the right track。
o You develop tests after the implementation of the Add() method as an exhaustive measure
to make sure you’ve dotted the i’s and crossed the t’s in the implementation。
Adding a Test Project to Your Solution
When writing test routines; you will need to organize the source code; and that means figuring
out to which project the tests are added。 For the calculator application; you could place the test
routines within the Calculator class library。 However; doing that is not the proper approach
due to distribution of the class library and correct testing context。 Remember that the test
routines must be identical to how the code will be used。 Thus; the appropriate place for the test
routines is in their own application。
The ideal approach is to create another application that represents the tests。 Figure 2…5
illustrated how a Windows and console application could use the Calculator class library。
Figure 2…9 adds the testing console application that also uses the class library。
Figure 2…9。 Adding the testing console application
The testing console application is like the console application created in Chapter 1; and it
references the Calculator class library。 Both projects should be part of the Calculator solution。
Go ahead and add the TestCalculator project to the Calculator solution。 Remember to
add a reference to the Calculator class library (right…click TestCalculator and choose Add
Reference Projects Calculator)。 Remember to set TestCalculator as the startup project for
debugging purposes。 Figure 2…10 shows the TestCalculator and Calculator projects in the
Solution Explorer。
…………………………………………………………Page 59……………………………………………………………
CH A PT E R 2 ■ L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 37
Figure 2…10。 Solution Explorer showing the testing console application and Calculator
class library
Testing Simple Addition
Add the boldfaced code to Module1。vb in the testing console project to verify the addition of 1
and 2 (the operator means is not equal to):
Imports Calculator
Module Module1
Public Sub TestSimpleAddition()
Dim result As Integer = Operations。Add(1; 2)
If (total 3) Then
Console。WriteLine(〃Oops 1 and 2 does not equal 3〃)
End If
End Sub
Sub Main()
TestSimpleAddition()
Console。ReadKey()
End Sub
End Module
…………………………………………………………Page 60……………………………………………………………
38 CH AP T E R 2 ■ L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S
Press Ctrl+F5 to test the calculation。 When executed; the testing console application calls
the test method TestSimpleAddition(); which calls and verifies the functionality of the Calculator
class library。 If everything works; the message will not appear。
■Note Recall that the Main() method is where a project begins its operation。 To make an application do
something; you must add code to its Main() method。
To see that the test also fails; change the Add() method as follows:
Public Shared Function Add(ByVal number1 As Integer; ByVal number2 As _
Integer) As Integer
Return number1 * number2
End Function
Now rerun the program; and you’ll see the failure message。
THE DEVELOPMENT CYCLE
So far in this chapter; we’ve dealt with three pieces of code:
o The code segment that implements the Add() method performs the calculation operation。
o The code that represents a caller; which could be either the Windows application or the console appli
cation; is considered production code。
o The code that contains the production code with some verification routines represents testing code。 The
testing code is important because