[Django]-How to assert exception using django.test.TestCase?

3👍

The error will be raised when you construct the Template object, not when you render it. You thus expect the error to raise with t.render(c), but the error is in the t = template.Template(…), so you need to wrap another statement in the context manager:

class TestTemplatePaginateTags(TestCase):
    def test_render_range_by_var_as_index_error(self):
        with self.assertRaises(template.TemplateSyntaxError):
            template.Template(
                "{% load pagination_tags %}{% autopaginate var by as %}{{ foo }}"
            )

Leave a comment