# Test module
import twill
from twill import commands as tc
from twill.shell import TwillCommandLoop
from django.test import TestCase
from django.core.servers.basehttp import AdminMediaHandler
from django.core.handlers.wsgi import WSGIHandler
from StringIO import StringIO
# Functions you'll need:
def twill_setup():
app = AdminMediaHandler(WSGIHandler())
twill.add_wsgi_intercept("127.0.0.1", 8080, lambda: app)
def twill_teardown():
twill.remove_wsgi_intercept('127.0.0.1', 8080)
def make_twill_url(url):
# modify this
return url.replace("http://www.yourwebsite.com/",
"http://127.0.0.1:8080/")
def twill_quiet():
# suppress normal output of twill.. You don't want to
# call this if you want an interactive session
twill.set_output(StringIO())
# Example tests. (This code has not been tested, as it
# would require a website to exist, but very similar
# code is working on my machine)
class MyTest(TestCase):
def setUp(self):
twill_setup()
def tearDown(self):
twill_teardown()
def test1(self):
url = "http://www.yourwebsite.com/signup/"
twill_quiet()
tc.go(make_twill_url(url))
tc.find("Sign up") # sanity check
# Fill in the form
tc.fv('1', 'user_name', 'newuser1')
tc.fv('1', 'email', 'user1@somewhere.com')
tc.submit()
tc.find("A confirmation email has been sent")
def unfinished_test(self):
# Some complicated set up here, perhaps
url = "http://www.yourwebsite.com/prefs/"
tc.go(make_twill_url(url))
# The following will launch into an interactive
# twill session
cmd = TwillCommandLoop()
cmd.cmdloop()
# -----------------------------------------------------------------
# In your settings.py used for testing, add this:
DEBUG_PROPAGATE_EXCEPTIONS = True
# This will allow exception generated in view code or templates to propagate,
# otherwise they are caught by Django in an unhelpful way. Available from
# trunk rev 7537
Comments
Thank you, this has been very helpful.
#
Thanks for useful code. But how to make it works with cookies?
#