RequestFactory versus TestClient

You may have noticed something funny about the last test: we made a request using the RequestFactory instance, passing it a path that does not exist in urls.py yet. Why did we have to do that?

Recall that a Django view is simply any function that accepts a request and returns a response. RequestFactory creates a bare-bones WSGIRequest for the first argument of a view without actually going through the Django framework (URL routing, middleware, and so on). We provide the first argument (path as a keyword) simply because you can't have a WSGIRequest without a path. The actual argument is irrelevant, unless you're using it in the course of your view logic (meaning both /solos/1/ and /solos/foo/ would have let the test ...

Get Test-Driven Development with Django now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.