我正在创建单元测试py文件(tests.py),然后我收到以下错误消息。 你能否告诉我如何解决它。
pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower)
pydev debugger: starting
Finding files... done.
Importing test modules ... Traceback (most recent call last):
File "C:\Development\eclipse-SDK-3.7-win32\plugins\org.python.pydev.debug_2.2.3.2011100616\pysrc\pydev_runfiles.py", line 307, in __get_module_from_str
mod = __import__(modname)
File "C:\Development\workspace37_pydev\pyproject\py23\service\rs\tests.py", line 4, in <module>
from django.test.testcases import TestCase
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\__init__.py", line 6, in <module>
from django.test.testcases import TestCase
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\testcases.py", line 10, in <module>
from django.db import transaction
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\db\__init__.py", line 9, in <module>
if not settings.DATABASE_ENGINE:
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\conf\__init__.py", line 28, in __getattr__
self._import_settings()
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\conf\__init__.py", line 59, in _import_settings
self._target = Settings(settings_module)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\conf\__init__.py", line 94, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'py23.settings' (Is it on sys.path? Does it have syntax errors?): No module named py23.settings
ERROR: Module: tests could not be imported (file: C:\Development\workspace37_pydev\pyproject\py23\service\rs\tests.py).
done.
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
对正确方向的任何指导表示赞赏。
UPDATES1 我添加了我的tests.py源代码
# -*- encoding:utf-8 -*-
from django.contrib.gis.geos import GEOSGeometry
from django.test.testcases import TestCase
from django.utils import simplejson as json
import unittest
import utils
class RSCapTestCase(TestCase):
fixtures = [ 'rs.json' ]
def test_001_normal(self):
response = self.client.get('/service/rs/cap', {})
cap = json.loads(response.content)
self.assertEqual(response.status_code, 200)
self.assertEqual(cap['version'], '1.0')
self.assertEqual(len(cap['rs']), 3)
self.assertEqual(cap['rs'][0], 'r1')
self.assertEqual(cap['updateSequence'], '0001')
def test_002_version(self):
response = self.client.get('/service/rs/cap/1.0', {})
cap = json.loads(response.content)
self.assertEqual(response.status_code, 200)
self.assertEqual(cap['version'], '1.0')
self.assertEqual(len(cap['rs']), 3)
self.assertEqual(cap['rs'][0], 'r1')
self.assertEqual(cap['updateSequence'], '0001')
def test_003_version_sequence(self):
response = self.client.get('/service/rs/cap/1.0/0001', {})
cap = json.loads(response.content)
self.assertEqual(response.status_code, 200)
self.assertEqual(cap['version'], '1.0')
self.assertEqual(len(cap['rs']), 3)
self.assertEqual(cap['rs'][0], 'r1')
self.assertEqual(cap['updateSequence'], '0001')
UPDATES2 我添加了以下代码
_ _ init _ _.py(与tests.py相同的文件夹)
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'py23.settings'
执行tests.py
时,这是控制台Finding files... done.
Importing test modules ... done.
======================================================================
ERROR: test_001_normal (py23.service.rs.tests.RSCapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\testcases.py", line 202, in __call__
self._pre_setup()
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\test\testcases.py", line 183, in _pre_setup
call_command('flush', verbosity=0, interactive=False)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\core\management\__init__.py", line 158, in call_command
return klass.execute(*args, **options)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\core\management\base.py", line 210, in execute
translation.activate('en-us')
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\__init__.py", line 73, in activate
return real_activate(language)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\__init__.py", line 43, in delayed_loader
return g['real_%s' % caller](*args, **kwargs)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\trans_real.py", line 209, in activate
_active[currentThread()] = translation(language)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\trans_real.py", line 198, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:\Program Files\GeoDjango\Django-1.0.2-final\django\utils\translation\trans_real.py", line 181, in _fetch
app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
AttributeError: 'module' object has no attribute 'messages'
.
.
.
----------------------------------------------------------------------
Ran 3 tests in 0.234s
FAILED (errors=43)
答案 0 :(得分:2)
以下是我在不对代码进行任何更改的情况下解决此问题的方法:
这至少对我有用。
答案 1 :(得分:0)
我只需添加以下内容:
import os
from os.path import abspath, dirname
import sys
# Set up django
project_dir = abspath(dirname(dirname(__file__)))
sys.path.insert(0, project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'py23.settings'