如何阻止我的上下文处理器破坏其他应用程序单元测试

时间:2011-10-17 10:26:11

标签: django testing

我有一个django项目,它包含几个项目,以及django.contrib.auth。当我运行python manage.py test并运行auth单元测试时,django似乎正在尝试评估我的所有上下文处理器,包括与其他应用程序相关的处理器。

我的另一个应用程序的上下文处理器之一使用reverse来查找链接的URL,并且在auth单元测试内部失败时导致这些测试失败。如何解决这个问题,以便我的上下文处理器不会干扰对不相关的应用程序运行单元测试?

感谢。

示例测试失败(我的其他应用程序称为'monitor',在monitor / context_processors.py中,尝试获取monitor.views.dashboard的URL)

======================================================================
ERROR: test_unknown_user (django.contrib.auth.tests.remote_user.RemoteUserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/django/contrib/auth/tests/remote_user.py", line 50, in test_unknown_user
    response = self.client.get('/remote_user/', REMOTE_USER='newuser')
  File "/usr/lib/python2.7/site-packages/django/test/client.py", line 445, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/usr/lib/python2.7/site-packages/django/test/client.py", line 229, in get
    return self.request(**r)
  File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.7/site-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/django/contrib/auth/tests/urls.py", line 13, in remote_user_auth_view
    c = RequestContext(request, {})
  File "/usr/lib/python2.7/site-packages/django/template/context.py", line 177, in __init__
    self.update(processor(request))
  File "/home/me/hydra/hydra-server/monitor/context_processors.py", line 13, in menu_items
    items.append({"url": reverse('monitor.views.dashboard'), "caption": "Dashboard"})
  File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 391, in reverse
    *args, **kwargs)))
  File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 337, in reverse
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'monitor.views.dashboard' with arguments '()' and keyword arguments '{}' not found.

1 个答案:

答案 0 :(得分:1)

  1. 您可以默默地NoReverseMatch(可能是记录错误)。
  2. 您可以在模板中将字符串传递给反向,然后使用{% url %}标记将其反转,而不是在上下文处理器中。
  3. 您可以忽略这一点,如果您没有修补它,需要测试Django吗?