Python CGI FieldStorage Test Harness

How to set up a small test harness for a Python CGI script? I do not want to start the server to test it, but I want to supply various GET / POST inputs for my test.

It seems to me that the FieldStorage (or the object behind it) is completely unchanged, so I don’t see how to transfer CGI data on the fly in a bundle.

+4
source share
2 answers

You can use a mocking library such as Mock to complete the task. For example, suppose you want to test the function_to_test function_to_test from your CGI script, you can write the unittest class as follows:

 import unittest import cgi from mock import patch def function_to_test(): form = cgi.FieldStorage() if "name" not in form or "addr" not in form: return "<H1>Error</H1>\nPlease fill in the name and address.\n" text = "<p>name: {0}\n<p>addr: {1}\n" return text.format(form["name"].value, form["addr"].value) @patch('cgi.FieldStorage') class TestClass(unittest.TestCase): class TestField(object): def __init__(self, value): self.value = value FIELDS = { "name" : TestField("Bill"), "addr" : TestField("1 Two Street") } def test_cgi(self, MockClass): instance = MockClass.return_value instance.__getitem__ = lambda s, key: TestClass.FIELDS[key] instance.__contains__ = lambda s, key: key in TestClass.FIELDS text = function_to_test() self.assertEqual(text, "<p>name: Bill\n<p>addr: 1 Two Street\n") def test_err(self, MockClass): instance = MockClass.return_value instance.__contains__ = lambda self, key: False text = function_to_test() self.assertEqual(text, "<H1>Error</H1>\nPlease fill in the name and address.\n") 

If I run this code as unit test, I get:

 .. ---------------------------------------------------------------------- Ran 2 tests in 0.003s OK 
+4
source

If you do not want to use an additional library such as Mock: you can create a cgi.FieldStorage object with some test data. The Python example below assumes that you are expecting POST input:

 import unittest from io import BytesIO class TestForm(unittest.TestCase): def setUp(self): """ Makes a cgi.FieldStorage object with some bogus fields. """ # provide a byte string with the parameters # using the format b"name1=value1&name2=value2..." urlencode_data = b"firstname=Joe&lastname=Bloggs& email=joe.bloggs@company.com " urlencode_environ = { 'CONTENT_LENGTH': str(len(urlencode_data)), 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'QUERY_STRING': '', 'REQUEST_METHOD': 'POST', } data = BytesIO(urlencode_data) data.seek(0) self.fs = cgi.FieldStorage(fp=data, environ=urlencode_environ) # unit test methods come here # form fields are accessible via `self.fs` 

The idea comes from https://bugs.python.org/file9507/cgitest.py . There you will find other interesting examples, for example. file upload forms, etc.

Please note that the __init__ cgi.FieldStorage method is undocumented or at least I could not find it in the current cgi module documentation .

+2
source

Source: https://habr.com/ru/post/1386011/


All Articles