4 Any assert statement will take the optional kwarg of 'msg'. This kwarg
5 will be output instead of an error in the test. It is a very useful
6 argument for debugging.
9 self.assertTrue(False, msg="And what else floats? A duck!")
14 Here are a few pointers for testing forms.
16 If there is an error in the test for form.is\_valid() a good way to find
17 out what form fields are giving trouble is by checking form.errors.
20 form = MyFormWithErrors()
21 self.assertTrue(form.is_valid(), msg=form.errors)
23 All Django forms generally accept 'initial', 'instance', and 'data'
24 keyword arguments, but forms will behave differently depending on which
25 of these arguments you pass in.
29 This is probably the easiest of all the form tests to work with, because
30 it is what you are so used to seeing.
33 # data is a dictionary
35 self.assertTrue(form.is_bound)
36 self.assertTrue(form.is_valid())
37 for field in form.fields:
38 self.assertEqual(data[field], form.cleaned_data[field])
42 Initial is a unique case in which I have not found an easy way to check
43 if the initial values of the form were properly set.
44 Checking against **form.fields[field].initial** will not work as this is
45 set to the value of the initial keyword argument originally passed into
49 # initial is a dictionary
50 form = MyForm(initial=initial)
51 self.assertFalse(form.is_bound)
52 self.assertFalse(form.is_valid())
53 for field in form.fields:
54 self.assertEqual(initial[field], form.fields[field].initial) # This will *NOT* work!
58 Forms that are passed the instance keyword argument will have set the
59 'instance' property on form.
60 Thus you can test form.instance against the instance you passed in.
63 # instance is an instance of a model
64 form = MyModelForm(instance=instance)
65 self.assertEqual(instance, form.instance)