Hey Simon,
Simon Willison wrote:
> The best way of running tests against a Django application, to my
> mind, is to run a "fake" web server (i.e. one that doesn't actually
> bind to a port) around the application. This fake server acts as a
> test harness. Tests can then create HttpRequest objects (probably a
> subclass called something like TestHttpRequest), pass them to the
> Django application, get back an HttpResponse object and run
> assertions against that.
>
> ...
I have implemented exactly that: assertions agains the context.
(And, what irony, I would have liked to show it to you during
EuroPython, but time was just too short ...)
I've attached my stuff to the ticket #2333. It'll certainly need
some clean-up for public use.
I didn't want to patch Django to fetch the context, so I resorted
to define a template tag (which has access to the context) to
store the context away in the thread local store.
A TestRunner class is used to run a test case (i.e., setting up
environment, getting response, calling middleware, storing the
cookies like a browser would). It's only a minimal browser
simulation, of course. It also contains the logic for comparing
actual with expected output.
It doesn't work too well to just write out the repr() of the
context and then write asserts that compare it to a string:
doctests show that this is limited, e.g. it does not work for
dicts. So I wrote a "mockup" utility that tries to mimic the
structure of containers. As opposed to what Simon wrote, I'm
usually testing the whole context, but this can be changed.
TestGhostWriter is a middleware that can create test cases for
you. It writes using the standard python logging interface.
A single test looks like this:
#
--------------------------------------------------------------------
# GET /mailadmin/mailbox-qsearch/
GET = MultiValueDict({'search': ['bert'], 'kunde': ['']})
POST = MultiValueDict({})
response, context =
runner.get_response('/mailadmin/mailbox-qsearch/', 'GET', GET, POST)
assert runner.check_response(response, {'status_code': 200,
'cookies': SimpleCookie('')})
assert runner.check_context(response, context, {'form':
{'_inline_collections': None,
'advisory_dict': {},
'data': MultiValueDict({'search': ['bert'], 'kunde':
['']}),
'edit_inline': True,
'error_dict': {},
'manipulator':
'<dj_kunde.mailadmin.forms.search.MailboxQuickSearchForm object>'},
'mailboxes': ['<Person: slu1, Adalbert>', '<Person: slu2,
Berta>'],
'messages': [],
'person': '<Person: bla, Herr X.Y.>',
'user': '<User: bla>'})
I'm not sure if my writing makes sense. I'll be happy to give
more information, just ask.
Michael
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---