Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created June 1, 2011 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codeinthehole/1002158 to your computer and use it in GitHub Desktop.
Save codeinthehole/1002158 to your computer and use it in GitHub Desktop.
Twill TestCase for django
class TwillTestCase(TestCase):
"""
Simple wrapper around Twill to make writing TestCases easier.
Commands availabel through self.command are:
- go -> visit a URL
- back -> back to previous URL
- reload -> reload URL
- follow -> follow a given link
- code -> assert the HTTP response code
- find -> assert page contains some string
- notfind -> assert page does not contain
- title -> assert page title
"""
HOST = '127.0.0.1'
PORT = 8080
def setUp(self):
app = AdminMediaHandler(WSGIHandler())
twill.add_wsgi_intercept(self.HOST, self.PORT, lambda: app)
twill.set_output(StringIO())
self.command = twill.commands
def tearDown(self):
twill.remove_wsgi_intercept(self.HOST, self.PORT)
def reverse(self, url_name, *args, **kwargs):
"""
Custom 'reverse' function that includes the protocol and host
"""
return 'http://%s:%d%s' % (self.HOST, self.PORT, reverse(url_name, *args, **kwargs))
def visit(self, url_name, *args,**kwargs):
self.command.go(self.reverse(url_name, *args, **kwargs))
def assertResponseCodeIs(self, code):
self.command.code(code)
def assertPageContains(self, regexp):
self.command.find(regexp)
def assertPageDoesNotContain(self, regexp):
self.command.notfind(regexp)
def assertPageTitleMatches(self, regexp):
self.command.title(regexp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment