Creating the dLL Test Script
It’s time to build an IronPython script to test the DLL shown in Listing 18-1. In this case, the test
script is a bit short and doesn’t test every contingency (such as starting with a negative amount in
the account), but it demonstrates how you’d create a test script for a DLL. Listing 18-2 contains the
code needed for this example.
LISTINg 18-2:
Developing a DLL test harness
# Creates a new heading.
def CreateHeading(Test, Title):
print ‘\n########################################‘
print ‘Test ID = ‘, Test
print ‘Test Title = ‘, Title
# Displays the values.
def ShowValues(Expected, Received):
print ‘Expected Value = ‘, Expected
print ‘Received Value = ‘, Received
if Expected == Received:
print ‘Test Passed’
else:
print ‘Test Failed’
# Ends the test.
def CreateFooter():
print ‘########################################‘
548592c18.indd 394
2/24/10 12:49:45 PM
www.finebook.ir
Testing DLLs
❘
395
# Print out statements of everything the test is doing.
print ‘Beginning Test’
print ‘Loading clr’
import clr
print ‘Loading test module’
clr.AddReferenceToFile(‘TestDLL.DLL’)
from TestDLL import *
CreateHeading(‘0001’, ‘Creating Account1’)
Account1 = Accounts()
ShowValues(5000, Account1.GetTotal)
CreateFooter()
CreateHeading(‘0002’, ‘Making a Deposit’)
Account1.Deposit = 1000
ShowValues(6000, Account1.GetTotal)
CreateFooter()
CreateHeading(‘0003’, ‘Making a Withdrawal’)
Account1.Withdrawal = 500
ShowValues(5500, Account1.GetTotal)
CreateFooter()
CreateHeading(‘0004’, ‘Creating Account2’)
Account2 = Accounts(3000)
ShowValues(3000, Account2.GetTotal)
CreateFooter()
CreateHeading(‘0005’, ‘Transferring Money’)
Account1.Transfer(Account2)
print ‘\nAccount1 = 8500’
ShowValues(8500, Account1.GetTotal)
print ‘\nAccount2 = 0’
ShowValues(0, Account2.GetTotal)
CreateFooter()
# Pause after the debug session.
raw_input(‘\nPress any key to continue...’)
Let’s begin with the three functions at the beginning of the script:
CreateHeading()
,
ShowValues()
,
and
CreateFooter()
. It may seem a bit silly at first to create these functions, but they provide a
method for changing the output of the tests quickly, should you need to do so. In addition, you don’t
want to write the same
print
statements hundreds of times as you create your script. It’s far easier to
simply call the functions.
The
CreateHeading()
and
CreateFooter()
functions don’t have much logic in them — they
simply display information onscreen. The
ShowValues()
function does have a bit of logic. In this
case, it simply compares the expected value to the result and displays the appropriate output text.
However, you could perform any number of checks required by your application. For example, if
you’re working with strings, you might need to check the string length and determine precisely how
it differs from another string.
548592c18.indd 395
2/24/10 12:49:45 PM
www.finebook.ir
Do'stlaringiz bilan baham: |