Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 31adbd2ce0
Fetching contributors…

Cannot retrieve contributors at this time

48 lines (27 sloc) 976 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.

  • Functional tests are slower, but catch more bugs.

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