[Tutor] A not-so-basic question...

Alan Gauld alan.gauld at freenet.co.uk
Tue Jan 4 13:28:17 CET 2005


> I don't quite grasp "automated testing".  And yes, I test in place
> constantly, but it only ever gets tedious when I cant figure out why
a
> thing doesn't work.  ;)

Think about writing a moduule and testing it at the >>> prompt.
What you are doing is typing a series of python commands into
the prompt to excercise the code you wrote and checking that
the result is what you expect.
So why not write a program to issue those same commands and
check the results match what you expect? Then anytime you
change your module you can run all the tests again in a
second or two instead of retyping all the tests over again.

Now if you run the original tets in something like IDLEs
shell window you can save the session and quickly edit it
into a set of tests with the results already there.

For example here is a fictitious session testing a fictitious
module:

>>> import mymodule as m
>>> testobject = m.myClass()
>>> type(testobject)
<type instance of MyClass>  # I said it was fictitious!!
>>> testobject.doit()
'lots of interesting output'
>>> testobject.doit(42)  # valid value
' more interesting stuff'
>>> testobject('ghfghfghf')  #invalid value
, exception raised by MyClass code'

Now if I save that and edit the file I can easily turn it into:

############
import time
print 'Starting tests for module MyClass at: ', time.time()

import mymodule as m
testobject = m.MyClass()

if type(testobject) == type(m.MyClass():
   print 'instantiation worked'
else: print 'ERROR: failed to instantiate correctly

if testobject.doit() == 'lots of interesting output':
    print 'default doit passed'
else: print 'ERROR: default doit failed'

if testobject.doit(42)  == ' more interesting stuff':
    print 'doit(42) passed'
else: print 'ERROR: doit(42) failed'

try:
    testobject.doit('ghfghfghf')  #invalid value
    print 'ERROR: doit failed to detect faulty data'
except MyClassException: print 'bad data detected OK'

print 'End of tests for module MyClass
#################

And after any changes to MyClass I can run this program and
get (hopefully!) output like:

Starting tests for module MyClass at 12:34pm
instantiation worked
default doit passed
doit(42) passed
Bad data detected OK
End of tests for module MyClass

Some people prefer to skip the print outs and just bomb out
with an error if a test fails or print a single "All tests passed"
at the end. Personally I like a little feedback, but if there's
too much output you have to analyse it for errors - Thats
why the error results have ERROR in big letters to spot it easily.

If you are making lots of changes this saves a lot of time
and effort and you are much more likely to do the tests than
if you had to type them all by hand each time! This is whats
referred to as regression testing - repeating past tests to
make sure changes haven't broken what used to work!

Now what I did is doing it the hard way, there are various tools
around to make the task easier, look at the unittest,
doctest and assert features of Python for some more ideas.

HTH

Alan G.



More information about the Tutor mailing list