如何在django unittest中测试django会话?

时间:2015-08-01 18:41:11

标签: python django unit-testing session django-1.8

我创建了视图:

def forgot_password(request):
"""
Actions when user forgot password.
:param request: object
:return: redirect to views.home() or 'loginsys/forgot.html' with form, message
"""
message = None
if request.method == 'POST':
    form = ForgotPasswordForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data['email']
        user = User.objects.filter(email=email)
        salt = random_salt(len(user[0].username))
        code = signing.dumps([user[0].id, user[0].email, user[0].username],
                             key=settings.SECRET_KEY, salt=salt)
        url = settings.SITE_URL + reverse('loginsys:reset', args=[code])
        send_email.apply_async(('Welcome', '<p>Hello</p><p><a href="{0}">Go to this link</a></p>'.
                                format(url), [email]))
        store = ForgotPasswordLink()
        store.random_salt = salt
        store.user_link_id = user[0].id
        store.code_value = code
        store.save()
        forgot_password_salt_life.apply_async((user[0].id, salt), countdown=180)
        request.session["message"] = 'Instruction have sent on your mail - {0}'.format(email)
        return redirect(reverse('home'))
else:
    form = ForgotPasswordForm()
return render(request, 'loginsys/forgot.html', {'form': form, 'message': message})

我在会话中写了留言:

request.session["message"] = 'Instruction have sent on your mail - {0}'.format(email)

然后我测试了视图:

def test_forgot_password(self):
    response = self.client.post(reverse('loginsys:forgot'), {'email': 'email@project.com'})
    self.assertRedirects(response, reverse('home'), 302, 200)
    session = self.client.session
    self.assertEqual(session['message'], 'Instruction have sent on your mail - email@project.com')

我有错误:

Error
Traceback (most recent call last):
  File "/home/maxim/PycharmProjects/SSHKeyStore/loginsys/tests.py", line 105, in test_forgot_password
    self.assertEqual(session['message'], 'Instruction have sent on your mail - email@project.com')
  File "/home/maxim/SSHKeyStoreVE/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 48, in __getitem__
    return self._session[key]
KeyError: 'message'

所以我不知道问题是什么。我找到了一些例子,并在那里制作,但它没有帮助: Django testing stored session data in testsHow do I modify the session in the Django test framework, https://stackoverflow.com/questions/25132621/setting-a-session-variable-in-django-tests错误:

Error
Traceback (most recent call last):
  File "/home/maxim/PycharmProjects/SSHKeyStore/loginsys/tests.py", line 22, in setUp
    self.client.session = self.session
AttributeError: can't set attribute

我收到了消息:

RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  return f(*args, **kwds)

这很糟糕。

请帮我解决问题并了解问题( 可能是解决方案在这里Using session object in Django unit test但我不明白。)

感谢。

1 个答案:

答案 0 :(得分:4)

为此目的定义一个mixin,就像这样

from django.test import Client
from importlib import import_module

class ModifySessionMixin(object):
    client = Client()

    def create_session(self):   
        session_engine = import_module(settings.SESSION_ENGINE)        
        store = session_engine.SessionStore()                          
        store.save()
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

现在从这个mixin扩展你的测试类,并在你的setUp方法中调用create_session来初始化session。

class TestMobileResumeUpload(ModifySessionMixin, TestCase):
    def setUp(self):
        ....
        self.create_session()
        ....

您现在可以将任何参数设置为self.client.session,您可以在视图中将其作为会话变量进行访问。此外,视图中完成的会话更改将在self.client.seesion中提供以进行测试。