mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14191 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
120aae2209
commit
eec45e8b71
4 changed files with 244 additions and 132 deletions
|
@ -1014,6 +1014,51 @@ The following is a simple unit test using the test client::
|
|||
# Check that the rendered context contains 5 customers.
|
||||
self.assertEqual(len(response.context['customers']), 5)
|
||||
|
||||
The request factory
|
||||
-------------------
|
||||
|
||||
.. Class:: RequestFactory
|
||||
|
||||
The :class:`~django.test.client.RequestFactory` is a simplified
|
||||
version of the test client that provides a way to generate a request
|
||||
instance that can be used as the first argument to any view. This
|
||||
means you can test a view function the same way as you would test any
|
||||
other function -- as a black box, with exactly known inputs, testing
|
||||
for specific outputs.
|
||||
|
||||
The API for the :class:`~django.test.client.RequestFactory` is a slightly
|
||||
restricted subset of the test client API:
|
||||
|
||||
* It only has access to the HTTP methods :meth:`~Client.get()`,
|
||||
:meth:`~Client.post()`, :meth:`~Client.put()`,
|
||||
:meth:`~Client.delete()`, :meth:`~Client.head()` and
|
||||
:meth:`~Client.options()`.
|
||||
|
||||
* These methods accept all the same arguments *except* for
|
||||
``follows``. Since this is just a factory for producing
|
||||
requests, it's up to you to handle the response.
|
||||
|
||||
Example
|
||||
~~~~~~~
|
||||
|
||||
The following is a simple unit test using the request factory::
|
||||
|
||||
from django.utils import unittest
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
class SimpleTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Every test needs a client.
|
||||
self.factory = RequestFactory()
|
||||
|
||||
def test_details(self):
|
||||
# Issue a GET request.
|
||||
request = self.factory.get('/customer/details')
|
||||
|
||||
# Test my_view() as if it were deployed at /customer/details
|
||||
response = my_view(request)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
|
||||
TestCase
|
||||
--------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue