Django RequestFactory
So far, we have been using Django’s test client to test the views that we have created for our application. The test client class simulates a browser and uses this simulation to make calls to the required APIs. But what if we didn’t want to use the test client and its associated simulation of being a browser, but rather wanted to test the view functions directly bypassing the request parameter? How can we do that?
To help us in such cases, we can leverage the RequestFactory
class provided by Django. The RequestFactory
class helps us provide the request object that we can pass to our view functions to evaluate whether they are working. The following object for the RequestFactory
class can be created by instantiating the class as follows:
factory = RequestFactory()
The factory object created only supports HTTP methods such as get()
, post()
, put()
, and others to simulate a call to any URL endpoint. Now, let’s learn how to modify the...