Search code examples
djangodjango-formsdjango-testingdjango-tests

Django form validation - assertFalse()


I am quite new to the Django testing software. Right at the moment I am trying to create a test class for validators connected with given form ( process of cleaning the input data):

forms.py:

class SignupForm(forms.Form):
    email = forms.EmailField(max_length=200)
    first_name = forms.CharField(max_length=200)
    last_name = forms.CharField(max_length=200)
    company_name = forms.CharField(max_length=200)
    password1 = forms.CharField(max_length=200)
    password2 = forms.CharField(max_length=200)

    def clean(self):
        cleaned_data = self.cleaned_data
        cleaned_username_obtained = cleaned_data.get('username')
        if(cleaned_username_obtained != None):
            username_obtained = cleaned_username_obtained.lower()
        else:
            raise ValidationError('Username is empty!')
        cleaned_email_address = cleaned_data.get('email')
        cleaned_password1 = cleaned_data.get('password1')
        cleaned_password2 = cleaned_data.get('password2')
        cleaned_first_name = cleaned_data.get('first_name')
        cleaned_last_name = cleaned_data.get('last_name')
        cleaned_company_name = cleaned_data.get('company_name')
        if username_obtained != None:
            if((User.objects.filter(username=username_obtained)).count() > 0):
                self._errors['username'] = [u'Username is already in use']
            elif((ModelOfAdvertisementClient.objects.filter(email_address=cleaned_email_address)).count() > 0):
                self._errors['email'] = [u'Email is already in use']
            elif(len(cleaned_password1) == 0):
                self._errors['password1'] = [u'Password input box is empty']
            elif(cleaned_password2 != cleaned_password1):
                self._errors['password2'] = [u'Inserted passwords are different!']
        return cleaned_data 

test_forms.py:

class SignupFormTest(TestCase):
    @classmethod

    def test_correct_values(self):
        form = SignupForm({
            'username': 'testUsername',
            'email': '[email protected]', 
            'first_name': 'Karl',
            'last_name': 'Smith',
            'company_name': 'HiTech Inc.',
            'password1': 'test123',
            'password2': 'test123'
            }
        )
        print(str(form.is_valid()))
        self.assertFalse(form.is_valid())

Unfortunately, each time I am launching written above code, it ends up with following error message:

self.assertFalse(form.is_valid())
TypeError: assertFalse() missing 1 required positional argument: 'expr'

and due to the fact, that

form.is_valid()

returns "False", I literally have no clue of what is wrong with this assert


Solution

  • Testing methods should not have @classmethod decorators. Those are meant for class-wide setup/teardown code. In your case because of @classmethod decorator you are silently calling an unbound assertFalse() method that expects 2 arguments: a TestCase instance an a boolean expression.