[Django]-How do I verify in a Django unit test that my context contains a form object?

3👍

The response has a context attribute that contains the context used to render the template.

self.assertIn('form', response.context)

0👍

It’s possible to use assertIn but it’s better to use assertContains .

self.assertContains(response, 'form', count=None, status_code=200, msg_prefix='', html=False)

This allow you to have more options and check status_code at the same time. for example set html to True to handle text as HTML. The comparison with the response content will be based on HTML semantics instead of character-by-character equality. more info here.

Leave a comment