How can I set up a small test harness for Python CGI script? I don’t want to run a server in order to test it, but I do want to supply various GET/POST inputs for my test.
It appears to me that FieldStorage (or the object behind it) is utterly immutable, so I don’t see how to supply the CGI data on the fly in a harness.
You could use a mocking library, such as Mock to do the job. For example, suppose you want to test the
function_to_testfunction from your CGI script, you could write a unittest class like this:If I run this code as a unit test I get: