Opened 16 years ago

Closed 13 years ago

Last modified 12 years ago

#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)

t9002.diff (17.7 KB ) - added by Russell Keith-Magee 13 years ago.
Implemenation of RequestFactory as base class for Client

Download all attachments as: .zip

Change History (11)

comment:1 by Zellyn Hunter, 15 years ago

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.

in reply to:  1 comment:2 by Zellyn Hunter, 15 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.

in reply to:  1 comment:3 by Zellyn Hunter, 15 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:4 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:5 by Jacob, 15 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Kevin Kubasik, 15 years ago

Owner: changed from nobody to Kevin Kubasik

comment:7 by Kevin Kubasik, 15 years ago

I have added support in django.test.mocks based on the snippet. Still needs docs and tests.

in reply to:  7 comment:8 by Peter Bengtsson, 14 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 Russell Keith-Magee, 13 years ago

Attachment: t9002.diff added

Implemenation of RequestFactory as base class for Client

comment:9 by Russell Keith-Magee, 13 years ago

Resolution: fixed
Status: newclosed

(In [14191]) Fixed #9002 -- Added a RequestFactory. This allows you to create request instances so you can unit test views as standalone functions. Thanks to Simon Willison for the suggestion and snippet on which this patch was originally based.

comment:10 by anonymous, 12 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

This needs some triage love

Note: See TracTickets for help on using tickets.
Back to Top