user3607370
user3607370

Reputation: 237

how to check the name of the template?

help please write unittest. it should load the address and determine the name of the template

test.py:

from django.utils import unittest
from django.test.client import Client

class SimpleTest(unittest.TestCase):
    def setUp(self):
        self.client = Client()

    def test_details(self):
        response = self.client.get('/accounts/login/')
        self.assertTemplateUsed(response, template_name, 'accounts/login.html') 

urls.py:

urlpatterns = patterns('app',
    url(r'^accounts/logout/$', 'views.logout', name='logout', ),    
    url(r'^accounts/login/$', 'views.login', name='login', ),
)

views.py:

def login(request):                 
    t = loader.get_template('accounts/login.html')
    c = RequestContext(request, {
        'form': form, 
    }, [custom_proc])   
    return HttpResponse(t.render(c)) 

the problem is that the console displays the following error message:

======================================================================
ERROR: test_details (app_drummersaransk.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Python33\django_projects\drummersaransk_new\app_drummersaransk\tests.
py", line 10, in test_details
    self.assertTemplateUsed(response, template_name, 'accounts/login.html')
AttributeError: 'SimpleTest' object has no attribute 'assertTemplateUsed'

----------------------------------------------------------------------
Ran 21 tests in 0.533s

FAILED (errors=1)
Destroying test database for alias 'default'...

c:\Python33\django_projects\drummersaransk_new>

Upvotes: 1

Views: 589

Answers (1)

alecxe
alecxe

Reputation: 473833

You are using unittest.TestCase class that doesn't have assertTemplateUsed assertion method.

Instead use django.test.TestCase as a base class for your test case:

from django.test import TestCase

class SimpleTest(TestCase):
    def setUp(self):
        self.client = Client()

    def test_details(self):
        response = self.client.get('/accounts/login/')
        self.assertTemplateUsed(response, 'accounts/login.html')

Upvotes: 3

Related Questions