Created
January 30, 2012 04:45
-
-
Save stephenmcd/1702592 to your computer and use it in GitHub Desktop.
Persistent Sessions in Django TestCase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, i have been looking for a solutions to this