I am trying to validate my forms that raise validation error.
My test is as follows:
def test_register_password_strength(self): form_params = {'first_name': 'John', 'last_name': 'Doe', 'email': ' john@doe.com ', 'password': 'a', 'password_confirm': 'a', 'g-recaptcha-response': 'PASSED'} form = RegisterForm(form_params) self.assertFalse(form.is_valid()) try: form.clean_password() self.fail('Validation Error should be raised') except ValidationError as e: self.assertEquals('pw_too_short', e.code)
And the form raises a ValidationError as follows:
password = forms.CharField(label='Password', widget=forms.PasswordInput) widgets = { 'password': forms.PasswordInput(), } def clean_password(self): password = self.cleaned_data.get('password') if len(password) < 7: raise ValidationError('Password must be at least 7 characters long.', code='pw_too_short') return self.cleaned_data.get('password')
self.assertFalse(form.is_valid()) correctly claims false, but when I try to call form.clean_password() , I get the following error: TypeError: object of type 'NoneType' has no len() .
self.cleaned_data does not have an element named password after calling form.is_valid() .
Is there any other way to test ValidationErrors besides calling is_valid() ?
source share