Functional testing in Pylons, Twill and wsgi_intercept
Posted by Graham Stratton Sun, 13 May 2007 20:08:00 GMT
When trying to do some functional testing in Pylons, I initially tried Paste’s solution, which worked a bit, but I got stuck on trying to select multiple options in a select box. So I decided to move over to Twill, which I am already familiar with anyway.
In order to use Twill for in-process testing, Titus Brown, the developer of Twill, has provided a cunning module called wsgi_intercept. Yes, there is a clue in the name. wsgi_intercept replaces httplib.HTTPConnection, redirecting specified requests to the wsgi application. Titus has an article on testing with Twill and wsgi_intercept.
Using the module with Pylons is quite easy. I created a new project called squirrel, with a controller called nuts. In squirrel/tests/init.py, I added:
import twill
class TwillTestController(TestCase):
wsgi_app = loadapp('config:test.ini', relative_to=conf_dir)
def setUp(self):
def build_app():
return self.wsgi_app
twill.add_wsgi_intercept('localhost', 8080, build_app)
def tearDown(self):
twill.remove_wsgi_intercept('localhost', 8080)
Then I changed squirrel/tests/functional/test_nuts.py to
from squirrel.tests import TwillTestController
from twill.commands import *
class TestNutsController(TwillTestController):
def test_index(self):
go('http://localhost:8080/')
find('World')
notfind('Universe')
And running nosetests works quite nicely for me. Unless, that is, http_proxy is set, which it is on my work machine. I haven’t yet traced this problem to see whether it can be fixed or not, though I suspect there won’t be a simple solution.
