2010-02-17 83 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

回答