#9002 closed Uncategorized (fixed)
Easy way to create mock Request objects for testing views
Reported by: | simon | Owned by: | Kevin Kubasik |
---|---|---|---|
Component: | Testing framework | Version: | 1.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The testing framework currently encourages hooking up a view function using a URLconf and then using the TestClient to run tests against the view.
It would be nice if there was an easy way to bypass URLconf entirely in testing and just create a mock object representing a GET / POST / etc request, then pass that directly to the view function.
See this thread: http://groups.google.com/group/django-developers/browse_thread/thread/db86050095ebe5db
And this snippet: http://www.djangosnippets.org/snippets/963/
Attachments (1)
Change History (11)
follow-ups: 2 3 comment:1 by , 16 years ago
comment:2 by , 16 years ago
After more experimentation it appears that a more complicated refactoring is necessary if this is to work with sessions, etc. The request middleware needs to run.
comment:3 by , 16 years ago
ps. it appears I accidentally messed with the needs_better_patch, needs_tests, and needs_docs settings - how do I tell what they were originally?
comment:5 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:6 by , 16 years ago
Owner: | changed from | to
---|
follow-up: 8 comment:7 by , 15 years ago
I have added support in django.test.mocks based on the snippet. Still needs docs and tests.
comment:8 by , 15 years ago
Replying to kkubasik:
I have added support in django.test.mocks based on the snippet. Still needs docs and tests.
Where? In a branch? I can't find anything in the trunk.
by , 14 years ago
Attachment: | t9002.diff added |
---|
Implemenation of RequestFactory as base class for Client
comment:9 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:10 by , 13 years ago
Easy pickings: | unset |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
This needs some triage love
I'd suggest creating new methods (preferably on the existing Client class - it's just a refactoring of the initial code from the existing methods), so that it would be easy to intermix use of the normal get() and post() methods and the new variations while sharing the same session data.