模拟Django插件:找不到settings.py

时间:2012-01-27 13:45:07

标签: django

我正在尝试编写一个名为Dynamic Response的django插件测试(可在GitHub上获得),但我遇到了一些问题。使用模拟我试图模拟某些Django方法,如render_to_response,(我毕竟不打算测试django)但是当我尝试这样做时,我收到一条错误消息,说它无法找到settings.py,它不应该是因为这是一个在其他项目中使用的插件,而不是一个项目本身。我尝试过以下代码:

class ResponseTest (unittest.TestCase):

    @patch('dynamicresponse.response.render_to_response')
    def test_is_testframework_working(self, render_to_response):
        from dynamicresponse.response import SerializeOrRender
        SerializeOrRender()

如上所述,它给出了一条错误消息。 Stubbing HttpResponse,HttpRedirect不会抛出这些错误......

给出以下stacktrace

Response (unittest.loader.ModuleImportFailure) ... ERROR

======================================================================
ERROR: response (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: response
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 252, in _find_tests
    module = self._get_module_from_name(name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
    __import__(name)
  File "/Users/skinney/Workspace/django-dynamicresponse/tests/response.py", line 5, in <module>
    class ResponseTest (unittest.TestCase):
  File "/Users/skinney/Workspace/django-dynamicresponse/tests/response.py", line 7, in ResponseTest
    @patch('dynamicresponse.response.render_to_response')
  File "/Users/skinney/Workspace/django-dynamicresponse/tests/mock.py", line 720, in patch
    target = _importer(target)
  File "/Users/skinney/Workspace/django-dynamicresponse/tests/mock.py", line 514, in _importer
    thing = _dot_lookup(thing, comp, import_path)
  File "/Users/skinney/Workspace/django-dynamicresponse/tests/mock.py", line 503, in _dot_lookup
    __import__(import_path)
  File "/Users/skinney/Workspace/django-dynamicresponse/dynamicresponse/response.py", line 3, in <module>
    from django.shortcuts import render_to_response
  File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py", line 10, in <module>
    from django.db.models.manager import Manager
  File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 14, in <module>
    if not settings.DATABASES:
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

1 个答案:

答案 0 :(得分:0)

你不能在没有任何项目(真实或虚拟,无论如何)的情况下测试django应用程序。