Package: django-forms-test
Module to simplify testing forms in Django frameworkInstallationYou can simply install it with use ofpip:pip installdjango-forms-testThen you’ll import it into your testing script like this:from django_forms_test import field, cleaned, FormTestUsageYou will create a subclass ofFormTestwhich is subclass ofdjango.test.TestCase, so you can add tests as you are used to. To
the attributeformyou assign class of the form you are testing.required_fieldsshould be a list of fields required by your form.classYourClass(FormTest):form=YourFormrequired_fields=['email','message']Instead of the name of a field you can use a tuple of name and field
type. Available field types areURL,TEXT,PASSWORD,USERNAME,EMAIL. Field types serves to additional checking of
permitted field value (e.g. a form shouldn’t accept an invalid URL). TheTEXTis the default type and has no additional verifications.classYourClass(FormTest):form=YourFormrequired_fields=[('email',field.EMAIL),'message']# same as ('message', field.TEXT)For testing of form’s cleaning methods you can use a decoratorcleanedwhich accepts the input field values as arguments and passes
them cleaned to your function.classYourClass(FormTest):form=YourForm@cleaned(nick='  Bob ')deftest_strip_nick(self,nick):self.assertEqual(nick,'Bob')ExampleYou code this:classSaveFormTest(FormTest):form=SaveFormrequired_fields=['title','tags',('url',field.URL)]@cleaned(tags='one, two')deftest_cleaning_tags(self,tags):self.assertIsInstance(tags,list)self.assertEqual(len(tags),2)self.assertEqual(tags[1],'two')@cleaned(description='')deftest_cleaning_description(self,description):self.assertIsNone(description)Instead of this:classSaveFormTest(TestCase):data={'title':'some title','url':'http://some.url','tags':'some tags',}deftest_valid_form(self):form=SaveForm(self.data)self.assertTrue(form.is_valid())deftest_invalid_url(self):my_data=copy(self.data)my_data['url']='invalid url'form=SaveForm(my_data)self.assertFalse(form.is_valid())deftest_required_fields(self):forfieldin('title','url','tags'):withself.subTest(field=field):my_data=copy(self.data)my_data[field]=''form=SaveForm(my_data)self.assertFalse(form.is_valid())deftest_cleaning_tags(self):form=SaveForm({'tags':'one, two'})form.is_valid()tags=form.cleaned_data['tags']self.assertIsInstance(tags,list)self.assertEqual(len(tags),2)self.assertEqual(tags[1],'two')deftest_cleaning_description(self):form=SaveForm({'description':''})form.is_valid()description=form.cleaned_data['description']self.assertIsNone(description)And you code this:classRegisterFormTest(FormTest):form=RegisterFormrequired_fields=[('username',field.USERNAME),('email',field.EMAIL),('password',field.PASSWORD),('confirm_password',field.PASSWORD),]Instead of this:classRegisterFormTest(TestCase):data={'username':'some_username','email':'some@email.com','password':'some password','confirm_password':'some password',}deftest_valid_form(self):form=RegisterForm(self.data)self.assertTrue(form.is_valid())deftest_invalid_username(self):my_data=copy(self.data)my_data['email']='invalid email'form=RegisterForm(my_data)self.assertFalse(form.is_valid())deftest_invalid_email(self):my_data=copy(self.data)my_data['email']='invalid email'form=RegisterForm(my_data)self.assertFalse(form.is_valid())deftest_required_fields(self):forfieldin('username','email','password','confirm_password'):withself.subTest(field=field):my_data=copy(self.data)my_data[field]=''form=RegisterForm(my_data)self.assertFalse(form.is_valid())deftest_password_safe_input(self):form=RegisterForm()input_type=form['password'].field.widget.input_typeself.assertEqual(input_type,'password')input_type=form['confirm_password'].field.widget.input_typeself.assertEqual(input_type,'password')AnnouncementI’ve done this just for my personal use, so there are many things that
are still missing and I probably won’t implement them until I need them.
However feel free to send a pull request if you miss something.