2010-12-15 56 views
20

我的网站允许个人在没有通过基于当前session_key可以创建一个用户被记录在贡献内容的会议如何修改Django的测试框架

我想建立一个测试我的查看,但它似乎是不可能修改的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,它似乎没有工作

+0

'session'是'dict'状物体,你应该使用'S [X]'来获取和设置值 – xj9 2014-10-03 18:57:33

回答

28

这就是我的做法(灵感来自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() 

等等

+0

这是否仍然适用于使用Pickle序列化器的django 1.6(而不是现在用于SESSION_SERIALIZER的标准JSONSerializer) – 2013-12-12 17:08:35

33

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/') 
+8

吕克您好,感谢您的帮助,但这不起作用。我收到以下错误: AttributeError:'字典'对象没有'保存'属性 这是因为client.session对象是字典。看来client.session只有在请求发出后才成为一个真正的Session对象(并且只有当用户登录时)。还有其他建议吗? 可以通过输入(来自壳)重复此:从django.test进口客户 客户 =客户端() S = client.session S [ 'EXPIRE_DATE'] = '2010-12-05' 小号['session_key'] ='my_session_key' s.save() – Riley 2010-12-20 15:24:55

+5

它看起来像luc建议应该工作,但它的django错误:https://code.djangoproject.com/ticket/11475 – 2012-06-15 20:25:39

+0

加1再次.. – 2013-03-04 21:53:30

3

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

与相应的网址查看:/虚拟/:

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/')

我也用这个虚拟视图通过测试时,用来初始化会话虚拟数据手。

10

安德鲁·奥斯汀已经提到的,它并没有因为这个错误的工作: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()创建并登录用户后,如上面的代码所示,会话应该可以工作。

0

按照该文档,您可以通过如

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()