Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make test case fail if a django template has a rendering error that would silently fail in production

I'm aware that django test cases are done with DEBUG=False and TEMPLATE_DEBUG=False, and that I can change it to True for a specific function, using

from django.test.utils import override_settings

@override_settings(DEBUG=True)
def test_one_function(self):
    # This test should be failing and is not.
    # If I did not test manually I would'nt know !
    pass

But maybe there is a better, more generic solution that apply for eveything at once ?

I have an error in my template : I included another template and the link is broken. If I manually check with DEBUG=True I get a TemplateDoesNotExist error. But during my test case the url is rendered without the broken include, it does not throw an error, and the http_status is 200. I already tested the very generic included template somewhere else, so I don't want to add test to see if what is inside was rendered correctly. But I want to see rendering fails, that's what my test are for !

I tried to set TEMPLATE_STRING_IF_INVALID to an Exception (found here), but it does not seem to be working for a broken include.

Is there a way to make all rendering error raise en exception during tests, without breaking the django's design principle of not running test in debug ?

like image 705
Pierre.Sassoulas Avatar asked Oct 19 '16 09:10

Pierre.Sassoulas


1 Answers

You can use --debug-mode to set DEBUG=True for all tests. Example:

$ ./manage.py test --debug-mode

Another solution is to wait for Django 2.1 (it is not released yet). Documentation promises that lost includes will not be silenced (see the end of include tag reference).

like image 184
user1266163 Avatar answered Oct 21 '22 03:10

user1266163