Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
Created January 30, 2012 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenmcd/1702592 to your computer and use it in GitHub Desktop.
Save stephenmcd/1702592 to your computer and use it in GitHub Desktop.
Persistent Sessions in Django TestCase
"""
The Django test client implements the session API but doesn't persist values in it:
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session
This Client subclass can be used to maintain a persistent session during test cases.
"""
from django.conf import settings
from django.test import TestCase
from django.test.client import Client
from django.utils.importlib import import_module
class PersistentSessionClient(Client):
@property
def session(self):
if not hasattr(self, "_persisted_session"):
engine = import_module(settings.SESSION_ENGINE)
self._persisted_session = engine.SessionStore("persistent")
return self._persisted_session
class MyTests(TestCase):
client_class = PersistentSessionClient
@Thompsonmina
Copy link

Thank you, i have been looking for a solutions to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment