If I have a test like so...
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
expected_html = render_to_string('home.html', request=request)
self.assertEqual(response.content.decode(), expected_html)
As soon as I add a form in curly braces, e.g. {{ form }}
The above test will fail as the .html file will not render the form only the response will. Thus causing the assertion to not match. Is there a way round this so that I can still test my html against the response?
You can pass a form instance to the
render_to_string
function:Usually what I do is splitting this kind of test into several other tests, like this:
Using
from django.test import TestCase
First test which template was used:
Test the form:
And then I test the key parts of the HTML:
Finally if the rendered template has a csrf token: