为什么 Django 项目 URL 并非全部可供 Django 测试客户端使用?
我一直在尝试将 django-lean 应用程序添加到我的项目中。 django-lean 应用程序不位于我正在处理的项目中,它位于 PYTHONPATH 上。
我无法通过 django-lean 测试。
问题似乎在于 TestCase 定义了 url 的值:
urls = 'django_lean.experiments.tests.urls'
据我所知,测试仅获取位于 @ 'django_lean.experiments.tests.urls' 的 url,而不是 项目其余部分的 url。
这会导致错误消息,例如:
NoReverseMatch: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
这些是由项目中的 {% url %} 模板标记触发的。
如何确保项目的所有 url 都可用于测试?
编辑: 有人向我展示了一个打印可见 URL 的脚本:
import urls
def show_urls(urllist, depth=0):
for entry in urllist:
print " " * depth, entry.regex.pattern
if hasattr(entry, 'url_patterns'):
show_urls(entry.url_patterns, depth + 1)
我从 ipdb 调用了该脚本,这是输出:
ipdb> import urls
ipdb> show_urls(urls.urlpatterns)
^test-experiment/(?P<experiment_name>.*)$
^test-clientsideexperiment/(?P<experiment_name>.*)$
^admin/
^(?P<experiment_name>.+)/$
^$
^main-app/
^goal/(?P<goal_name>.*)$
^confirm_human/$
这对应于位于 @ 'django_lean.experiments.tests.urls' 的
urlpatterns = patterns('django_lean.experiments.tests.views',
url(r'^test-experiment/(?P<experiment_name>.*)$', 'experiment_test'),
url(r'^test-clientsideexperiment/(?P<experiment_name>.*)$', 'clientsideexperiment_test'))
urlpatterns += patterns('',
url(r'^admin/', include('django_lean.experiments.admin_urls')),
url(r'^main-app/', include('django_lean.experiments.urls')),
URL我的问题是,我的测试全部失败,因为项目中其他应用程序的命名 url 由 URL 模板标签调用,测试无法访问。
我正在运行 Python 2.7 和 Django 1.2.1
I've been trying to add the django-lean app to my project.
The django-lean app is not located in the project I'm working on, it is on the PYTHONPATH.
I have not been able to get the django-lean tests to pass.
It seems the issue is that the TestCase defines a value for urls:
urls = 'django_lean.experiments.tests.urls'
As best as I can tell, the tests are only getting the urls located @ 'django_lean.experiments.tests.urls', but not
the urls from the rest of the project.
This is causing error messages like:
NoReverseMatch: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
These are triggered by {% url %} template tags in the project.
How can I make sure that the all the urls of the project are available for the tests?
EDIT:
Someone showed me a script to print the visible URLs:
import urls
def show_urls(urllist, depth=0):
for entry in urllist:
print " " * depth, entry.regex.pattern
if hasattr(entry, 'url_patterns'):
show_urls(entry.url_patterns, depth + 1)
I called this script from ipdb, this was the output:
ipdb> import urls
ipdb> show_urls(urls.urlpatterns)
^test-experiment/(?P<experiment_name>.*)$
^test-clientsideexperiment/(?P<experiment_name>.*)$
^admin/
^(?P<experiment_name>.+)/$
^$
^main-app/
^goal/(?P<goal_name>.*)$
^confirm_human/$
This corresponds to the urls located @ 'django_lean.experiments.tests.urls'
urlpatterns = patterns('django_lean.experiments.tests.views',
url(r'^test-experiment/(?P<experiment_name>.*)
The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.
I'm running Python 2.7 with Django 1.2.1
, 'experiment_test'),
url(r'^test-clientsideexperiment/(?P<experiment_name>.*)
The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.
I'm running Python 2.7 with Django 1.2.1
, 'clientsideexperiment_test'))
urlpatterns += patterns('',
url(r'^admin/', include('django_lean.experiments.admin_urls')),
url(r'^main-app/', include('django_lean.experiments.urls')),
The issue that I'm having is that my tests all fail because of the named urls from other apps in the project are called by URL template tags are not accessible to the tests.
I'm running Python 2.7 with Django 1.2.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案非常简单。只需将主项目中的 URL 导入到应用程序的 urls.py 中即可。
或者对于更通用的解决方案:
The solution was pretty simple. Just import the URLs from the main project into the urls.py for the app.
or for a more generic solution:
要列出 django 知道的所有 url 模式,您可以使用此处建议的答案。
从您的测试中运行它并打印/记录输出。
请注意,最好明确说明从何处导入 url,
因为您可能还有一些包含 url 文件的其他模块。
To list all the url patterns your django knows you can use the answer suggested here.
Run this from your tests and print/log the output.
Just note that its better to explicitly state where to import the urls from like
because you probably have some other modules containing urls files.