Search code examples
djangodjango-viewssetcookietestcase

How to access cookies in Django TestCase request and responses?


I have a view that sets a cookie using response.set_cookie method. I would like to test if the cookie is being set in a TestCase.

According to docs, the cookie should be accessible in the client object, but client.cookies.items returns an empty list. The cookie is being correctly set in the browser.

Test case:

>>> response = self.client.get(url)
>>> self.client.cookies.items()
[]

Any ideas?


Solution

  • You need to use the response's client instance:

    response = self.client.get(url)
    response.client.cookies.items()