如何在Django测试框架中修改会话

问题描述 投票:25回答:6

我的网站允许个人在没有登录的情况下通过基于当前session_key创建用户来贡献内容

我想为我的视图设置一个测试,但似乎无法修改request.session:

我想这样做:

from django.contrib.sessions.models import Session
s = Session()
s.expire_date = '2010-12-05'
s.session_key = 'my_session_key'
s.save()
self.client.session = s
response = self.client.get('/myview/')

但我得到错误:

AttributeError: can't set attribute

关于如何在获取请求之前修改客户端会话的想法?我见过this它似乎不起作用

python django django-testing django-sessions
6个回答
32
投票

这就是我做到的方式(灵感来自于http://blog.mediaonfire.com/?p=36的解决方案)。

from django.test import TestCase
from django.conf import settings
from django.utils.importlib import import_module

class SessionTestCase(TestCase):
    def setUp(self):
        # http://code.djangoproject.com/ticket/10899
        settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
        engine = import_module(settings.SESSION_ENGINE)
        store = engine.SessionStore()
        store.save()
        self.session = store
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

之后,您可以创建测试:

class BlahTestCase(SessionTestCase):

    def test_blah_with_session(self):
        session = self.session
        session['operator'] = 'Jimmy'
        session.save()

等等...


46
投票

django测试框架的客户端对象使触摸会话成为可能。请查看http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session了解详情

小心:To modify the session and then save it, it must be stored in a variable first (because a new SessionStore is created every time this property is accessed)

我认为下面这样的事情应该有效

s = self.client.session
s.update({
    "expire_date": '2010-12-05',
    "session_key": 'my_session_key',
})
s.save()
response = self.client.get('/myview/')

11
投票

正如安德鲁·奥斯汀已经提到过的那样,由于这个错误它不起作用:https://code.djangoproject.com/ticket/11475

你可以做的是:

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

class SessionTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        User.objects.create_user('john', '[email protected]', 'johnpassword')
        self.client.login(username='john', password='johnpassword')

    def test_something_with_sessions(self):
        session = self.client.session
        session['key'] = 'value'
        session.save()

使用User.objects.create_user()和self.client.login()创建并登录用户后,如上面的代码所示,会话应该有效。


3
投票

您可以创建插入虚拟数据(如会话)的自定义视图。

具有相应url的视图:/ dummy /:

def dummy(request):
    # dummy init data
    request.session['expiry_date'] = '2010-12-05'
    return HttpResponse('Dummy data has been set successfully')

比在测试脚本中只需调用self.client.get('/dummy/')

我还使用这个虚拟视图在手动测试时初始化会话中的虚拟数据。


1
投票

根据文档,您可以通过例如向测试客户端中的会话添加值

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

https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Client.session

这将允许您测试需要会话中的数据才能正常运行的视图。

所以对于这个问题:

session = self.client.session
   session['expire_date'] = '2010-12-05'
   .....
   session.save()

0
投票

您可以使用middleware修改会话。

© www.soinside.com 2019 - 2024. All rights reserved.