unit testing CGI scripts?

Myles myles at geocities.com
Tue Feb 10 18:35:33 EST 2004


> Does anyone have suggestions for unit testing CGI scripts?  For
> instance, how can I set the FieldStorage to have certain values?
> Do I have to go with a dummy FieldStorage class?  And is there a

I use 2 fairly simple and naive techniques within CGI scripts 
(not proper unittests, I'll be the first to admit, but perhaps you can
work them into proper unittests):

>From scratch:

testing = 1
if testing :
    year = "2003"
    week = "13"
else :
    form = cgi.FieldStorage()
    year = form["year"].value
    week = form["week"].value

or sometimes, when retrofitting testing:

testing = 1
if testing :
    class fakefield:
        def __init__(self, startingvalue):
            self.value = startingvalue
    yr = fakefield("2001")
    wk = fakefield("13")
    form = {"year":yr, "week":wk}
else:
    form = cgi.FieldStorage()

# the existing code
year = form["year"].value
week = form["week"].value

All final scripts are wrapped in a try/except structure that
automatically sends me an email if an uncaught exception occurs.
Python is just so tasty for quick meals.

Regards, Myles.



More information about the Python-list mailing list