No forms exist in unit test with django-webtest

422 Views Asked by At

I want to write a test that will test change the password in the application. I use the django-allauth. For testing, I use django-WebTest.

When I run my code, I get the message:

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

My code:

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

There are 1 best solutions below

0
On

WebTest tests the rendered content. There is no form found in the HTML (maybe the form tag is incorrect or missing).

If it's working in your manual test. You can try to print the response to see what's different:

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