如何检查模板的名称?

时间:2014-05-27 17:38:22

标签: python django unit-testing testing django-templates

帮助请写unittest。它应该加载地址并确定模板的名称

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)) 

问题是控制台显示以下错误消息:

======================================================================
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>

1 个答案:

答案 0 :(得分:3)

您使用的unittest.TestCase类没有assertTemplateUsed断言方法。

而是使用django.test.TestCase作为测试用例的基类:

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')