在Djangoallauth中测试更改密码时不存在表单

2024-04-24 23:30:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个测试,将测试更改应用程序中的密码。 我用的是django allauth。 对于测试,我使用djangowebtest。你知道吗

当我运行我的代码时,我得到一个消息:

FAILED (errors=1)
Destroying test database for alias 'default'...
(urlop)mariusz@mariusz-K73E:~/myapp$ python manage.py test users
Creating test database for alias 'default'...
.E
======================================================================
ERROR: test_password_change_use_template (myapp.users.tests.ChangePasswordTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mark/myapp/users/tests.py", line 16, in test_password_change_use_template
    password_change.form['oldpassword'] = "test123"
  File "/home/mark/.virtualenvs/urlop/local/lib/python2.7/site-packages/webtest/response.py", line 56, in form
    "You used response.form, but no forms exist")
TypeError: You used response.form, but no forms exist

这是我的测试代码:

from django_webtest import WebTest
from django_dynamic_fixture import G
from users.models import User
from django.core.urlresolvers import reverse

class ChangePasswordTest(WebTest):
    def setUp(self):
        self.user = G(User)

    def test_password_change_code(self):
        password_change = self.app.get(reverse('account_change_password'), user=self.user)

    def test_password_change_use_template(self):
        password_change = self.app.get(reverse('account_change_password'), user=self.user)
        password_change.form['oldpassword'] = "test123"
        password_change.form['password1'] = "test456"
        password_change.form['password2'] = "test456"
        password_change.form.submit()
        self.assertRedirects(password_change, reverse('change_password'))

Tags: djangofrompytestimportselfformuse