【问题标题】:how to check the name of the template?如何查看模板的名称?
【发布时间】:2014-05-27 17:38:22
【问题描述】:

帮助请编写单元测试。它应该加载地址并确定模板的名称

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>

【问题讨论】:

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


    【解决方案1】:

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

    改为使用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')
    

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2012-11-13
      • 2017-07-04
      • 2021-09-06
      相关资源
      最近更新 更多