testing django with twill and nosetests
Posted by Andrey Khavryuchenko on 04 Jun 2007 at 09:17 pm | Tagged as: Blog, django if (function_exists('social_bookmark')) { social_bookmark(); } ?>
Just a little writeup..
Now have a pleasure of not starting django development webserver, nor using apache to do http-level tests in my project.
Thanks to twill and its wsgi integration.
Works smoothly now, but caused me little cursing when twill refused to see rss feed.
After digging the twill code I’ve changed a single line - still don’t know how correct is this:
--- /usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/wsgi_intercept.py.orig 2007-06-04 21:10:37 +0300
+++ /usr/lib/python2.4/site-packages/twill-0.9b1-py2.4.egg/twill/wsgi_intercept.py 2007-06-04 21:10:45 +0300
@@ -265,7 +265,7 @@
for data in self.write_results:
self.output.write(data)
- if generator_data:
+ if generator_data is not None:
self.output.write(generator_data)
for data in self.result:
self.output.write(data)
Nevertheless it didn’t break anything and I have a luxury to test rss feeds too:
# -*- coding: utf-8 -*-
# $Id: tests.py 326 2007-06-04 15:58:48Z akhavr $
from nose.tools import *
from twill.commands import *
from twill import add_wsgi_intercept
def setup():
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "web.settings"
from django.core.servers.basehttp import AdminMediaHandler
from django.core.handlers.wsgi import WSGIHandler
app = AdminMediaHandler(WSGIHandler())
add_wsgi_intercept("127.0.0.1", 9876, lambda: app)
@with_setup(setup)
def test_rss():
'test rss feed'
go('http://127.0.0.1:9876/kds-djangofeed/feed/rss/')
code('200')
find('<rss version="2.0"><channel><title>Django Feed Planet.</title>')
return
14 Responses to “testing django with twill and nosetests”
Leave a Reply
You must be logged in to post a comment.








This works fine as you describe, except that for some reason I get the page content twice for every page.
I made a test view which only returns HttpResponse(’TEST’), and executing go(’http://127.0.0.1:9876/test/’) ; show() prints out “TESTTEST”.
Also showforms() for a simple one-form page displays the form twice.
I tried bypassing twill’s own browser and using twill._browser.MechanizeBrowser — same result.
What are the exact versions of Django and twill you used? I could try them first and, if they work ok, go on upgrading both gradually to newer versions to see if the problem appears at some point.
akaihola,
I’m using quite recently svn trunk of django.
I’ve not seen the behaviour you describe, so can’t help right now.
Does ’show’ itself dumps the content twice? Or something before it?
Try debugging the issue..
My twill is twill-0.9b1-py2.4.egg
I have Django newforms-admin branch rev 6710 and twill 0.9b1.
Something behind show() is doubling the content. As I said, I tried connecting directly with mechanize:
It\’s interesting that the \”closing…\” message is printed twice. Going one step deeper, I got the same result by calling b.mechopen(url) instead of b.open(url).
Continuing the debug session…
akaihola, please share the results of your debug session. I’ve not observed such behaviour and wonder what should cause it. On the other hand, I don’t check the output manually, I use regexp to match predefined criteria, so I might easily miss that.
I’m using django trunk (not newforms-admin) and now have r6702 - I update only every other week or so.
Looks like it’s an issue either with twill’s mechanize or django itself.
I set debuglevel to 2 (verbose) in twill.wsgi_intercept.py, and this is what it spits out:
Getting close…
(sorry, I didn’t know how to mark up code, there’s no syntax reference link here)
akaihola, I’ve fixed the formatting. Prefix each code line with 4 spaces or single tab - and it will be formatted like code.
Re log itself: obviously, the django method is called twice for some reason.
Ok, I see where this is happening. The
method first writes page headers and payload from generator_data, then from self.result.
I\\’m not familiar enough with WSGI to be able to analyze whether it\\’s Django or twill which is behaving incorrectly.
Here are some pointers to the relevant code on both sides:
In twill, see http://darcs.idyll.org/~t/projects/twill/twill/wsgi_intercept.py
around line 260 (their trac site seems to be down).
In Django, see the call method at http://code.djangoproject.com/browser/django/trunk/django/core/handlers/wsgi.py#L186
The startresponse() function from line 225 of wsgiintercept.py ends up as the start_response argument of WSGIHandler.call().
Here is a super-simple test case. Put the files twilltest.py and urls.py in a directory, make sure Django and twill are in PYTHONPATH and run the test by typing “PYTHONPATH=. python twilltest.py” in the directory.
twilltest.py:
urls.py:
Output on my Linux box with both Python 2.4 and 2.5 and twill 0.9b1:
I asked for help on the django-developers newsgroup:
http://groups.google.com/group/django-developers/t/c02ebeb8e9910953
The twill site seems to be down right now, so I’ll have to approach them later.
I did try my test case with today’s up-to-date checkout of both Django trunk and newforms-admin. Both double the page content.
Result of further investigation:
twill 0.8.4 worked correctly, 0.8.5 introduced this duplicating behavior.
Good news: Titus promptly tracked down a bug in twill, and this issue is now fixed in twill-latest.
The package is available at http://darcs.idyll.org/~t/projects/twill-latest.tar.gz
Download the tarball and run:
easy_install twill-latest.tar.gz
(if you have setuptools installed)