Search code examples
djangotestingdjango-testing

Django test client ignores "include" terms in Django template


I have an issue with Django tests client. Let for the home path I have this template (home.html):

<html>
    <body>
       {% include 'example.html' %}
    </body>
</html>

and in example.html I have error:

<div>
   {% non_registered_tag arg1 arg2 %}
</div>

I wrote a test for accessibility to a Django URL.

class HomePageAccess(TestCase):
  def test_home_page(self):
    client = Client()
    response = client.get(reverse_lazy('home'))
    self.assertEqual(response.status_code, 200)

This code fails successfully if there be an error in home.html, But if there be an error in example.html which is included in home.html Test will pass even though we expect to fail because I included it in home.html and in the browser I encounter an error (status code 500) while this not happens in test client.

Is it normal? I'm using Django 2.0.2. Any help will be appreciated


Solution

  • I suspect what's happening here is that you have the debug option set to False in the OPTIONS section in the TEMPLATES setting, or you have omitted it entirely (in which case it assumes the value of the overall DEBUG setting).

    Explicitly setting debug to True should expose the error and your test should fail as expected.

    TEMPLATES = [
        {
            ...
            'OPTIONS': {
                'debug': True,
            },
        },
    ]
    

    More information on the debug setting can be found in the template section of the Django docs here.