Opened 7 years ago

Closed 5 years ago

Last modified 4 years ago

#9002 closed Uncategorized (fixed)

Easy way to create mock Request objects for testing views

Reported by: simon Owned by: kkubasik
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 russellm 5 years ago.
Implemenation of RequestFactory as base class for Client

Download all attachments as: .zip

Change History (11)

comment:1 follow-ups: Changed 7 years ago by zellyn

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

comment:2 in reply to: ↑ 1 Changed 7 years ago by zellyn

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 in reply to: ↑ 1 Changed 7 years ago by zellyn

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 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:5 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 6 years ago by kkubasik

  • Owner changed from nobody to kkubasik

comment:7 follow-up: Changed 6 years ago by kkubasik

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

comment:8 in reply to: ↑ 7 Changed 6 years ago by peterbe

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.

Changed 5 years ago by russellm

Implemenation of RequestFactory as base class for Client

comment:9 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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 Changed 4 years ago by anonymous

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

This needs some triage love

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