2010-02-17 82 views
2

我遇到了一个问题......我们正在使用django编写项目,并且我正在尝试使用django.test.client与鼻子测试框架进行测试。django测试客户端问题

我们的代码是这样的:

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": "[email protected]", "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()的数据,它以正确的方式工作。

任何想法我可以如何解决这种情况?

+1

去看看http://stackoverflow.com/questions/2240067/why-does -nose-not-see-any-of-my-environmental-variables或者http://stackoverflow.com/questions/1484293/how-do-i-use-pymock-and-nose-with-django-models – 2010-02-17 10:39:57

+0

Gabriel thanx,这些解决方案看起来很清楚。 之前提出问题,我试图添加到测试文件字符串的开头: os.environ [“DJANGO_SETTINGS_MODULE”] =“smakly.frontend.settings” 但仍然没有帮助。在阅读你的链接后,我从bash中导出这个变量 - 它有帮助。合乎逻辑的问题 - 我如何从python测试文件中进行这种导出? – 2010-02-17 10:55:55

+0

有同样的问题,我只是删除settings.pyc文件,并重新启动服务器和错误消失...奇怪的东西;) – derevo 2012-01-05 05:18:39

回答