[Django]-How to use session in TestCase in Django?

30👍

Unfortunately, this is not a easy as you would hope for at the moment. As you might have noticed, just using self.client.session directly will not work if you have not called other views that has set up the sessions with appropriate session cookies for you. The session store/cookie must then be set up manually, or via other views.

There is an open ticket to make it easier to mock sessions with the test client: https://code.djangoproject.com/ticket/10899

In addition to the workaround in the ticket, there is a trick that can be used if you are using django.contrib.auth. The test clients login() method sets up a session store/cookie that can be used later in the test.

If you have any other views that sets sessions, requesting them will do the trick too (you probably have another view that sets sessions, otherwise your view that reads the sessions will be pretty unusable).

from django.test import TestCase
from django.contrib.auth.models import User

class YourTest(TestCase):
    def test_add_docs(self):
        # If you already have another user, you might want to use it instead
        User.objects.create_superuser('admin', 'foo@foo.com', 'admin')

        # self.client.login sets up self.client.session to be usable
        self.client.login(username='admin', password='admin')

        session = self.client.session
        session['documents_to_share_ids'] = [1]
        session.save()

        response = self.client.get('/')  # request.session['documents_to_share_ids'] will be available

40👍

As of Django 1.7+ this is much easier. Make sure you set the session as a variable instead of referencing it directly.

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'
    session.save()

andreaspelme’s workaround is only needed in older versions of django. See docs

👤Bufke

7👍

If you need to initialize a session for the request in tests to manipulate it directly:

from django.contrib.sessions.middleware import SessionMiddleware
from django.http import HttpRequest


request = HttpRequest()

middleware = SessionMiddleware()
middleware.process_request(request)

request.session.save()

0👍

If testing my django project with pytest, I can not see any modifications to the session that are made in the view. (That is because the Sessions middleware doesn’t get called.)

I found the following approach to be useful:

from unittest.mock import patch
from django.test import Client
from django.contrib.sessions.backends import db

def test_client_with_session():
    client = Client()
    session = {}  # the session object that will persist throughout the test
    with patch.object(db, "SessionStore", return_value=session):
        client.post('/url-that-sets-session-key/')

        assert session['some_key_set_by_the_view']

        client.get('/url-that-reads-session-key/')

This approach has the benefit of not requiring database access.

-2👍

You should be able to access a Client‘s session variales through its session property, so I guess self.client.session['documents_to_share_ids'] = [1] should be what you are looking for!

Leave a comment