Opened 10 years ago
Closed 10 years ago
#23606 closed New feature (fixed)
RequestFactory does not have a trace method.
Reported by: | Kévin Etienne | Owned by: | Rigel Di Scala |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | test |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When testing for views, RequestFactory
is very useful to get
, post
, put
, patch
, delete
, head
, options
a view.
However trace
is missing from RequestFactory
even if it's allowed by django.views.generic.View
.
A simple fix would be to add trace
to RequestFactory
.
Example (taken from the doc):
rf = RequestFactory() get_request = rf.get('/hello/') post_request = rf.post('/submit/', {'foo': 'bar'})
I have one case where I'm extending django.views.generic.View
and where I'm only allowing dispatch
to only use get
and I've written a test to check this condition.
Example:
My view:
from django.views.generics import View class CustomView(View): http_method_names = ('get',)
My test:
from django.test import RequestFactory, TestCase class TestCustomView(TestCase): method_not_allowed = ( 'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace', ) def test_method_not_allowed(self): url = '/' for method in method_not_allowed: with self.subTest(method=method): request = getattr(RequestFactory(), method)(url) ... self.assert(status_code, 405)
The previous test when hit return the following error:
Traceback (most recent call last): File "test_views.py", line 85, in test_method_not_allowed request = getattr(RequestFactory(), method)(url) AttributeError: 'RequestFactory' object has no attribute 'trace'
Change History (8)
comment:1 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Version: | 1.7 → master |
comment:2 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 10 years ago
comment:4 by , 10 years ago
Has patch: | set |
---|
comment:5 by , 10 years ago
Needs documentation: | set |
---|
The patch looks good (I just made a minor comment about using del ...
instead of del(...)
).
It just needs a mention in the release notes for 1.8 and we're good to go.
Thanks!
comment:6 by , 10 years ago
While I was adding the notes in the (django.test.RequestFactory)https://docs.djangoproject.com/en/1.7/topics/testing/advanced/#django.test.RequestFactory, I noticed that the django.test.Client, that subtypes the RequestFactory, does not implemenent the trace() method either. We would probably need to implement this method in the test client also.
What is the suggested course of action?
comment:7 by , 10 years ago
Needs documentation: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:8 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
The trace() method was not implemented in RequestFactory, and the RequestFactoryTests suite was testing just the GET requests.
I have submitted https://github.com/django/django/pull/3353 , that implements the trace() method, extends the scope of the existing test suite for the RequestFactory, and adds a test for the new method.