How to unit test the innards of a Django view function

15 November 2008   3 comments   Django

Mind That Age!

This blog post is 9 years old! Most likely, its content is outdated. Especially if it's technical.

Powered by Fusion×

Seconds ago I got this running and haven't yet fully understood what I've just done but the results are exactly what I need and this is going to be great.

Basically, in Django you have views like this:

def _render(template, data, request):
   return render_to_response(template, data,

def club_page(request, year):
   variable1 = year / 4
   variable2 = variable1 * 2
   return _render("foo.html", locals(), request)

Now in my unit tests I don't want to have to call the view function and then have to dissect the resulting HTML just to figure out if the view function prepared the correct variables. So, here's my solution to this problem:

In '':

from my_app.views import club_page

# mutable globals for capturing the traffic through _render
snatched_templates = []
snatched_data = []
snatched_requests = []
def _fake_render(template, data, request):
   return render_to_response(template, data,

class FakeRequest(object):
   def __init__(self, get=None, post=None, user=None):
       self.get = get = post
       self.user = user

   def GET(self):
       return self.get

   def POST(self):

class ViewsTestCase(unittest.TestCase):
   def test_club_page(self):

        # add a test user
       from django.contrib.auth.models import User
       user = User.objects.create_user(username='t', password='t', 

       # prepare a request 
       request = FakeRequest(user=staff)

       from my_app import views
       views._render = _fake_render

       response = club_day_page(request, 'fool', 2004)
       assert response.count('</html>')
       assert snatched_data[-1]['variable2'] == 102

This isn't really something a novice test driven Django developer can just copy since I've scribbled this code as pseudo code. My code for my app is a bit more complicated than this but the principle is the same.

So, tell me Django gurus, have I reinvented the wheel here or are other people finding this useful?


There is a more convenient way but it's quite arcane. After a client request has been made the response has an attribute context that you can use. E.g.:

response = client.get('/club/Test_club2/attendance-report/')
context_data = response.context[-1].dicts[0]

This approach, unlike my hack mentioned first, however requires that you have a publishable URL first.

Thanks Stephen!


James Bennett
The Django unit-testing documentation covers much easier ways to do this. For real-world examples, see the unit-test suite in the in-progress rewrite of my user-registration app:
Peter Bengtsson
404 on that.
Thank you for posting a comment

Your email will never ever be published

Related posts

Why do you vote for the democrats? 03 November 2008
domstripper - A lxml.html test project 20 November 2008
Related by Keyword:
Gamification for me as a software developer 21 December 2012
Mocking os.stat in Python 08 November 2009
Django vs. Java 25 October 2008
Google London Automation Test conference 08 September 2006
Related by Text:
My tricks for using AsyncHTTPClient in Tornado 13 October 2010
Mocking a Python standard library 14 March 2008
Goodies from tornado-utils - part 1: TestClient 20 September 2011
Integrate BrowserID in a Tornado web app 22 November 2011
Test static resources in Django tests 02 June 2011