Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (26 sloc) 923 Bytes

<!SLIDE incremental>

Unit testing views is hard.

  • Views have many collaborators / dependencies.

  • Templates, database, middleware, url routing...

  • Write less view code!

.notes Views have access to everything, so code in views is easy to write but hard to maintain and debug.


If you unit test views

  • Use RequestFactory.

  • Call the view callable directly.

  • Set up dependencies explicitly (e.g. request.user, request.session).


@@@ python
def test_change_locale(self):
    """POST sets 'locale' key in session."""
    request = RequestFactory().post(
        "/locale/", {"locale": "es-mx"})
    request.session = {}


        request.session["locale"], "es-mx")

<!SLIDE incremental>

Or don't.

  • I rarely unit test views.

  • I write less view code, and cover it via functional tests.

Jump to Line
Something went wrong with that request. Please try again.