django测试客户端的麻烦

时间:2010-02-17 10:37:42

标签: python django

我遇到了问题...我们正在使用django编写项目,我正在尝试使用django.test.client和nose test-framework进行测试。

我们的代码是这样的:

from simplejson import loads
from urlparse import urljoin

from django.test.client import Client

TEST_URL = "http://smakly.localhost:9090/"

def test_register():

    cln = Client()

    ref_data = {"email": "unique@mail.com", "name": "Василий", "website": "http://hot.bear.com", "xhr": "true"}

    print urljoin(TEST_URL, "/accounts/register/")
    response = loads(cln.post(urljoin(TEST_URL, "/accounts/register/"), ref_data))

    print response["message"]

在鼻子输出中我抓住了:

Traceback (most recent call last):
  File "/home/psih/work/svn/smakly/eggs/nose-0.11.1-py2.6.egg/nose/case.py", line 183, in runTest
    self.test(*self.arg)
  File "/home/psih/work/svn/smakly/src/smakly.tests/smakly/tests/frontend/test_profile.py", line 25, in test_register
    response = loads(cln.post(urljoin(TEST_URL, "/accounts/register/"), ref_data))
  File "/home/psih/work/svn/smakly/parts/django/django/test/client.py", line 313, in post
    response = self.request(**r)
  File "/home/psih/work/svn/smakly/parts/django/django/test/client.py", line 225, in request
    response = self.handler(environ)
  File "/home/psih/work/svn/smakly/parts/django/django/test/client.py", line 69, in __call__
    response = self.get_response(request)
  File "/home/psih/work/svn/smakly/parts/django/django/core/handlers/base.py", line 78, in get_response
    urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
  File "/home/psih/work/svn/smakly/parts/django/django/utils/functional.py", line 273, in __getattr__
    return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

我的settings.py文件确实具有此属性。

如果我从标准urllib2.urllopen().read()的服务器获取数据,它将以正确的方式工作。

我有什么想法可以解决这个案子?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

无耻的自我推销:正是出于这些原因,我制作了测试库,使您能够使用urllib2测试应用程序。

文档在那里:http://readthedocs.org/docs/django-sane-testing/en/latest/

您可能想要做的例子是,例如in django-http-digest-tests