unit testing CGI scripts?

J

Jim Hefferon

Hello,

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
better way to check the output than to compare the resulting (sometimes
very large) strings? Changing the HTML slightly means that I have to
edit the tests and that defeats the purpose of the tests, to
some extent. I'd greatly appreciate any tips.

Thanks,
Jim
 
M

Myles

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top