Richard Jones' Log: Webunit in action

Wed, 29 Oct 2003

Previously I'd only used webunit in unit testing scripts. Recently I wanted to hit Zope's session handling system hard (Anthony had concerns about its stability under load). So I wrote the following:

import sys
from webunit.webunittest import WebFetcher

browser = WebFetcher()
page = browser.get('http://localhost:8080/test/one')
n = int(page.body)
while 1:
    page = page.get('/test/two')
    m = int(page.body)
    assert m == n+1
    n = m
    sys.stdout.write('\r%s'%n);sys.stdout.flush()

Inside Zope, I have two pyscripts: "one" which sets a session value to 1:

SESSION = container.REQUEST.SESSION
SESSION['testing'] = 1
return SESSION['testing']

and "two" which increments the session value and returns it:

SESSION=container.REQUEST.SESSION
SESSION['testing'] = SESSION['testing'] + 1
return SESSION['testing']

Because webunit seamlessly handles cookies, the above test script works as expected. I ran several of the scripts simultaneously, and there were no issues.

Can't think how much pain I'd have to go through to write that test if I didn't have webunit :)