使用django-webtest进行单元测试时不存在任何表格

时间:2015-02-12 07:49:56

标签: django unit-testing django-allauth django-webtest

我想编写一个测试来测试更改应用程序中的密码。我用的是django-allauth。为了测试,我使用django-WebTest。

当我运行我的代码时,我收到消息:

FAILED (errors=1)
Destroying test database for alias 'default'...
mark@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'))

1 个答案:

答案 0 :(得分:0)

WebTest测试呈现的内容。在HTML中找不到任何表单(表单标签可能不正确或缺失)。

如果它在您的手动测试中工作。您可以尝试打印响应,看看有什么不同:

def test_password_change_use_template(self):
    response = self.app.get(reverse('account_change_password'), user=self.user)
    print response