Easily create mock request objects, for use in testing (original) (raw)
Author:
Posted:
August 12, 2008
Language:
Version:
.96
Score:
20 (after 20 ratings)
Django's testing framework assumes you will be running your tests against "live" views that have been plugged in to your site's URL configuration - but sometimes you might want to run a test against a view function without first wiring it in to the rest of the site. This class makes it easy to do that by providing a "factory" for creating mock request objects, re-using the existing test Client interface (and most of the code). Once you've created a request object in your test you can use it to call your view functions directly, then run assertions against the response object that gets returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | from django.test import Client from django.core.handlers.wsgi import WSGIRequest class RequestFactory(Client): """ Class that lets you create mock Request objects for use in testing. Usage: rf = RequestFactory() get_request = rf.get('/hello/') post_request = rf.post('/submit/', {'foo': 'bar'}) This class re-uses the django.test.client.Client interface, docs here: http://www.djangoproject.com/documentation/testing/#the-test-client Once you have a request object you can pass it to any view function, just as if that view had been hooked up using a URLconf. """ def request(self, **request): """ Similar to parent class, but returns the request object as soon as it has created it. """ environ = { 'HTTP_COOKIE': self.cookies, 'PATH_INFO': '/', 'QUERY_STRING': '', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': 'testserver', 'SERVER_PORT': 80, 'SERVER_PROTOCOL': 'HTTP/1.1', } environ.update(self.defaults) environ.update(request) return WSGIRequest(environ) |
---|
More like this
- Mask sensitive data from logger by agusmakmun 1 month, 2 weeks ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 3 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 4 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 10 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 11 months ago