naveen.panwar
naveen.panwar

Reputation: 403

Django: Testing if the template is inheriting the correct template

In django you can test weather your view is rendering correct template like this

    def test_view_renders_correct_template(self):
        response = self.client.get("/some/url/")
        self.assertTemplateUsed(response, 'template.html')

but what if you want to test if the used template is extending/inheriting from the correct template.

Upvotes: 3

Views: 1167

Answers (1)

vishes_shell
vishes_shell

Reputation: 23554

So as @e4c5 noted it's the same assertTemplateUsed.

Just tested it:

app/views.py

from django.shortcuts import render_to_response


def base_index(request):
    return render_to_response('base.html')


def template_index(request):
    return render_to_response('template.html')

app/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^base$', views.base_index, name='base'),
    url(r'^template$', views.template_index, name='template')
]

templates/template.html

{% extends 'base.html' %}
{% block content %}
  <div>help</div>
{% endblock %}

app/tests.py

from django.test import TestCase


class TemplateTest(TestCase):
    def test_base_view(self):
        response = self.client.get('/base')
        self.assertTemplateUsed(response, 'base.html')
        self.assertTemplateNotUsed(response, 'template.html')

    def test_template_view(self):
        response = self.client.get('/template')
        self.assertTemplateUsed(response, 'template.html')
        self.assertTemplateUsed(response, 'base.html')

All 2 tests passed

Upvotes: 7

Related Questions