unit testing CGI scripts?
Richard Wesley
hawkfish at trustedmedianetworks.com
Wed Feb 11 12:27:17 EST 2004
In article <mailman.1415.1076423956.12720.python-list at python.org>,
"Lee, Dustin" <dlee at rightnow.com> wrote:
> How about testing CGI scripts that access a database (I expect this is the
> norm)?
>
> I find writing unittests with non-cgi and non-database code very beneficial
> and pretty straight forward. I have yet to figure out how to do this with
> cgi and database coding. I'm starting to appreciate why pure functional
> languages (Haskell, etc) really laud the praises of doing "side-effect free"
> programming. Of course it's hard conceive of using a database with out
> actually changing it....
>
> So who out there is doing cgi/db programming and actually testing it?
Layering is very helpful. Have the cgi script entrypoint routine
initialize a configuration class containing all the relevant information
(database connection, host configuration, etc.). Then call a routine
that does all the work and returns the output in some structured
representation (e.g. a dictionary). Then have another standard routine
that generates the output from that representation and maybe a style
sheet.
To test the code, you now need to check three interfaces: the
configuration object constructor, the page generator and the cgi
implementation code. The tests for the first two can be separate from
the tests for an individual cgi entrypoint. To test a particular API,
all you need to do then is construct a variant of the configuration
object that uses predefined data for each setUp e.g. it talks to a clean
test database, it uses a clean section of the filesystem (I use
tempdirs), it has "fake" hostname information (I use
unittest.ourdomain.com). Then you can look at the structured output, do
diffs on the database, explore the filesystem and so on.
As you can see, the old dictum that you should design your code to be
tested comes in here. Good luck!
HTH,
--
- rmgw
<http://www.trustedmedianetworks.com/>
----------------------------------------------------------------------------
Richard Wesley Trusted Media Networks, Inc.
"'You don't know how to manage Looking-glass cakes,' the Unicorn remarked.
'Hand it round first, and cut it afterwards.'"
- Lewis Carroll, _Through The Looking-Glass And What Alice Found There_
More information about the Python-list
mailing list