Created
June 1, 2011 11:49
-
-
Save codeinthehole/1002158 to your computer and use it in GitHub Desktop.
Twill TestCase for django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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