【发布时间】:2010-02-17 10:37:41
【问题描述】:
我有一个问题...我们正在使用 django 编写项目,我正在尝试使用 django.test.client 和鼻子测试框架进行测试。
我们的代码是这样的:
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() 从服务器获取数据,它会以正确的方式工作。
有什么办法可以解决这个问题吗?
【问题讨论】:
-
Gabriel,thanx,这些解决方案看起来很清楚。在问问题之前,我试图添加到测试文件字符串的开头: os.environ["DJANGO_SETTINGS_MODULE"] = "smakly.frontend.settings" 但仍然没有帮助。阅读您的链接后,我从 bash 导出了这个变量 - 它有帮助。和逻辑问题 - 我怎样才能从 python 测试文件中进行这样的导出?
-
遇到了同样的问题,我只是删除了 settings.pyc 文件并重新启动服务器,错误就消失了……奇怪的东西;)