After successful POST a redirect should happen (PRG Pattern)
response = admin_client.post(url, data)
assert response.status_code == 302
If this test fails, I get a very meaningless error message.
AssertionError assert 200 == 302
Since the status is 200
, I know that the form data which I send did not validate.
Is there a django-way to get the error message of the django form validation into the exception?
Example: I would like to see something like "foo is require" (if "foo" is not in data
)
Django's custom TestCase class is a subclass of unittest.TestCase
and all of its assert functions have a msg
argument which when passed are used as the error message when an assertion fails. Also since you want the forms error to be displayed, you can get the form instance from the context and get its errors. Something like this should work:
assertEqual(response.status_code, 302, str(response.context['form'].errors))
Note forms also have form.errors.as_data()
which will show the exceptions raised during form validation, or form.errors.as_json()
any of which you might find more usable. Reference Form.errors