如何制作测试网址?

时间:2014-10-23 14:33:09

标签: python django django-urls

请帮助纠正测试。

我需要测试网址。

urls.py:

urlpatterns = patterns('app_zapsum',
    url(r'^$', 'views.rules', name='rules'),
)

views.py:

def rules(request): 
    t = loader.get_template('page_rules.html')
    c = RequestContext(request, {}) 

    return HttpResponse(t.render(c)) 

tests.py:

from django.test import TestCase, RequestFactory
from app_zapsum.views import rules
from django.core.urlresolvers import reverse 
from django.core.urlresolvers import resolve

class TestUrls(TestCase):
    def test_details(self):
        resolver = resolve('/')
        self.assertEqual(resolver.rules, rules) 

由于命令python manage.py test显示以下错误消息:

======================================================================
ERROR: test_details (app_zapsum.tests.TestUrls)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kalinins/.virtualenvs/zapsum/zapsum/app_zapsum/tests.py", line 47, in test_details
    self.assertEqual(resolver.rules, rules)
AttributeError: 'ResolverMatch' object has no attribute 'rules'

----------------------------------------------------------------------
Ran 3 tests in 0.068s

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

1 个答案:

答案 0 :(得分:1)

urlresolvers.resolve返回urlresolvers.ResolverMatch个对象。该对象具有func对象:

class TestUrls(TestCase):
    def test_details(self):
        resolver = resolve('/')
        self.assertEqual(resolver.func, rules)