2012-01-27 71 views
0

我正在嘗試編寫名爲Dynamic Response(可在GitHub上)的django插件的測試,但我遇到了一些問題。使用模擬我試圖模擬某些Django方法,如render_to_response,(我不打算測試Django畢竟),但當我嘗試這樣做時,我收到一條錯誤消息,說它找不到settings.py,它它不應該是因爲這是一個插件用於其他項目,而不是一個項目本身。我試過以下代碼:嘲笑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() 

其中,如上所述,給出了錯誤消息。打樁的HttpResponse,HttpRedirect不會拋出這些錯誤......

給出了下面的堆棧跟蹤

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) 
+0

你可以發佈錯誤消息/追蹤? – Banjer 2012-01-27 13:52:16

+0

添加評論。 – 2012-01-27 14:03:41

+0

我最終編寫了單元測試,作爲動態響應示例項目的一部分。 – 2012-02-06 01:10:37

回答

0

您不能測試Django的應用,而無需任何項目(真實或虛擬,什麼)有自己的設置文件。