How to test templates

Recently I had a bug where I forgot to close some curly brackets:

{% extends 'base.html'

Of course the response from the test was green -> response.status = 200 also all Items where printed on the page.

So is there a way to catch this or a Linter or something like Black for django temlates?

I’m surprised that wasn’t a syntax error in the template because of the unclosed tag. Django should probably detect that.

But for testing - I’m not aware of any linter or similar. But I have used the coverage plugin to ensure all lines of templates have been run during tests: https://pypi.org/project/django-coverage-plugin/ . Not sure it would have caught your case though.

1 Like

It just renders ugly:


but its not an syntax error and if I would test if the elements are on the page.

So this might be an unexpected behavior for Django.

The template coverage plugin slated my coverage percentage :laughing:

Yeah - maybe it should be!