Skip to content

Instantly share code, notes, and snippets.

@booxood
Last active December 26, 2015 12:39
Show Gist options
  • Save booxood/7152928 to your computer and use it in GitHub Desktop.
Save booxood/7152928 to your computer and use it in GitHub Desktop.
django test case add session,cookies 使用django测试框架,模拟web请求时,带session,cookies。
from django.test import TestCase
from django.conf import settings
from django.utils.importlib import import_module
class SessionTestCase(TestCase):
def setUp(self):
# http://stackoverflow.com/questions/4453764/how-do-i-modify-the-session-in-the-django-test-framework
# http://code.djangoproject.com/ticket/10899
# settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file' #use django settings
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store.save()
self.session = store
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
def test_add_session(self):
session = self.session
session['operator'] = 'Jimmy'
session.save() # add session
self.client.cookies['sr'] = 'qq' # add cookies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment