1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | # 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/")
# 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.set_output(StringIO()) # hush!
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