Skip to main content
 首页 » 编程设计

python之NoseTests 的基本设置

2024年11月01日8lyj

NoseTests 的文档看起来非常简单,但我无法在任何地方找到初始设置的答案。我已经完成 pip install nosetests 并且一切顺利,但是当我在我的 django 项目的根目录下键入“nosetests”时,我得到以下信息:

ERROR: Failure: ImproperlyConfigured (Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName 
    addr.filename, addr.module) 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath 
    return self.importFromDir(dir_path, fqname) 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir 
    mod = load_module(part_fqname, fh, filename, desc) 
  File "/Users/aronchick/Code/ayafi/pages/tests.py", line 1, in <module> 
    from django.test import TestCase 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module> 
    from django.test.client import Client, RequestFactory 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/django/test/client.py", line 21, in <module> 
    from django.db import close_connection 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module> 
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES: 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__ 
    self._setup(name) 
  File "/Users/aronchick/Code/ayafi/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in _setup 
    % (desc, ENVIRONMENT_VARIABLE)) 
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 

很明显,它没有在 Django 应用程序中获取设置,但不清楚原因 - 我可以使用 python manage.py 服务器或工头在本地正常运行我的应用程序。我错过了什么?

请您参考如下方法:

那是因为nose没有办法自己找到settings模块。您应该尝试插入 django-nose