[TIP] Getting started writing DocTests for Web applications

Fran Boon flavour at partyvibe.com
Wed Jan 21 13:47:42 PST 2009

Olemis Lang wrote:
> it is particularly useful to write doctests and load
> them like if you were using unittest ... with some beneficial
> side-effects, like storing the evaluation of a single interactive
> example in a single TestResult instance, and a few more ... ;)
>> Inspired by this post:
>> http://agiletesting.blogspot.com/2006/04/in-process-web-app-testing-with-twill.html
> This is great ... I'll try to write a simple test like the one
> mentionned in this article, but using dutest ... I'll let you know
> about the results once I run a test suite so as to tell you about the
> details ... hopefully soon ;)

Sounds good - looking forward to seeing simple examples :)

>> I'll report back when I've started to make real progress...

ok, I have managed to get some basic doctests running fine in this style 
using CherryPy's WebTest.

Create a simple module 's3_test':
import wsgi_intercept.webtest_intercept
class WSGI_Test(wsgi_intercept.webtest_intercept.WebCase):
     HTTP_CONN = wsgi_intercept.WSGI_HTTPConnection
     HOST = 'localhost'
     PORT = 8000
     def __init__(self):
         "Copy Intialise"
     def setUp(self):
         "Hook into the WSGI process"
         wsgi_intercept.add_wsgi_intercept(self.HOST, self.PORT, create_fn)
     def runTest(self):
         "Mandatory method for all TestCase instances"

Then in the doctests (in Controllers):
     >>> from s3_test import WSGI_Test
     >>> test=WSGI_Test()
     >>> '200 OK' in test.getPage('/sahana/cr/shelter')
     >>> test.assertHeader("Content-Type", "text/html")
     >>> test.assertInBody('List Shelters')
     >>> '200 OK' in test.getPage('/sahana/cr/shelter?format=csv')
     >>> test.assertHeader("Content-Type", "text/csv")

This is useful, but I can't seem to add support for the database calls 
so hard to extend this further.

I tried to refactor into a UnitTest, but that isn't working currently 

Right now I'm focussing on a different approach: Selenium

I have integrated Selenium core into the application which works 
We can use make_selenium.py to develop the scripts in Python yet execute 
in TestRunner:

I next want to capture the results for display in Bitten.
Anyone have a postresults.py?
I am looking to see if I can de-Plone this one: 
or convert this PHP one:

I also have Selenium RC working with the same tests (help from: 
Although the export from Selenium IDE doesn't like my custom 
user-extensions.js...need to talk to Selenium flks about that :/
This can then be potentially used by a Bitten-slave (on machines with 
graphical browsers anyway ;) )

Comments on this approach welcomed - what do I lose by focussing on 
The in-process testing?


More information about the testing-in-python mailing list