用于django url测试的resolve()不起作用

时间:2016-07-23 11:28:01

标签: python django tdd

我对网址解析器进行了测试,但它没有工作,我也不知道为什么。

urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('blog.urls')),
]

博客/ urls.py

from django.conf.urls import url
from . import views

app_name = 'blog'

urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^post/new/$', views.post_new, name='post_new'),
]

博客/ test.py

from django.core.urlresolvers import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.template.loader import render_to_string

from .views import post_list, post_new
class PostNewTest(TestCase):
    def test_root_url_resolve_to_post_list_view(self):
        found = resolve('post/new')
        self.assertEqual(found.func, post_new)

但是测试失败了:

(tdd_djangogirls) Chois@Chois-MacPro tdd_djangogirls $./manage.py test
Creating test database for alias 'default'...
...E
======================================================================
ERROR: test_root_url_resolve_to_post_list_view (blog.tests.PostNewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Chois/Workspace/django/tdd_djangogirls/blog/tests.py", line 23, in test_root_url_resolve_to_post_list_view
    found = resolve('post/new')
  File "/Users/Chois/.virtualenvs/tdd_djangogirls/lib/python3.5/site-packages/django/core/urlresolvers.py", line 534, in resolve
    return get_resolver(urlconf).resolve(path)
  File "/Users/Chois/.virtualenvs/tdd_djangogirls/lib/python3.5/site-packages/django/core/urlresolvers.py", line 405, in resolve
    raise Resolver404({'path': path})
django.core.urlresolvers.Resolver404: {'path': 'post/new'}

它有什么问题?

我也使用found = resolve('/post/new')进行了测试,但它也没有用。

0 个答案:

没有答案