2012-02-24 62 views
0

我想測試一個帶有自己設置模塊的小型可重用應用程序。全局(項目)設置可在應用程序設置中訪問,以支持變量的覆蓋,例如,使用Django testrunner測試可重複使用的Django應用程序

# in <my_app>/settings.py 
from django.conf import settings 
MY_SETTING_VAR = getattr(settings, 'MY_OVERRIDDEN_VAR', False) 

當我運行manage.py測試MYAPP我得到的測試如下:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

什麼是運行在這種情況下測試的正確方法?

+0

我即將做出一個愚蠢的評論,但是,你看看其他軟件包如何做到這一點? – pkoch 2012-02-25 05:38:35

回答

1

我不知道,我測試你貼什麼和它的工作對我來說:

<<< 12:18.25 Fri Feb 24 2012!~/testproject 
<<< [email protected]!10019 env 
>>> ./manage.py test testapp     
Creating test database for alias 'default'... 
Destroying old test database 'default'... 
. 
---------------------------------------------------------------------- 
Ran 1 test in 0.000s 

OK 
Destroying test database for alias 'default'... 
<<< 12:18.27 Fri Feb 24 2012!~/testproject 
<<< [email protected]!10020 env 
>>> cat testapp/tests.py 
from django.test import TestCase 

from .settings import * 

class SomeTestCase(TestCase): 
    def testSomething(self): 
     self.assertEqual(MY_SETTING_VAR, 'default') 
<<< 12:18.30 Fri Feb 24 2012!~/testproject 
<<< [email protected]!10021 env 
>>> cat testapp/settings.py 
from django.conf import settings 
MY_SETTING_VAR = getattr(settings, 'MY_OVERRIDDEN_VAR', 'default') 

你要確保你的實際代碼匹配這個工作的代碼。

應用程序最好包含一個展示應用程序或至少允許測試的虛擬項目。例如:

<<< 12:42.56 Fri Feb 24 2012!~/testproject/testapp 
<<< [email protected]!10034 E:1 env 
>>> pip install -e [email protected]:subsume/django-subscription.git#egg=sub 
Obtaining sub from [email protected]:subsume/django-subscription.git#egg=sub 
    Cloning [email protected]:subsume/django-subscription.git to /home/jpic/env/src/sub 
    Running setup.py egg_info for package sub 

Installing collected packages: sub 
    Running setup.py develop for sub 

    Creating /home/jpic/env/lib/python2.7/site-packages/django-subscription.egg-link (link to .) 
    Removing django-subscription 0.0 from easy-install.pth file 
    Adding django-subscription 0.1 to easy-install.pth file 

    Installed /home/jpic/env/src/sub 
Successfully installed sub 
Cleaning up... 
<<< 12:43.08 Fri Feb 24 2012!~/testproject/testapp 
<<< [email protected]!10035 env 
<<< 12:43.11 Fri Feb 24 2012!~/testproject/testapp 
<<< [email protected]!10035 env 
>>> cd ../../env/src/sub 
<<< 12:43.15 Fri Feb 24 2012!~/env/src/sub 
<<< [email protected]!10036 G:master env 
>>> ls 
django_subscription.egg-info docs README setup.py subscription subscription_test_project 
<<< 12:43.16 Fri Feb 24 2012!~/env/src/sub 
<<< [email protected]!10037 G:master env 
>>> cd subscription_test_project 
<<< 12:43.20 Fri Feb 24 2012!~/env/src/sub/subscription_test_project 
<<< [email protected]!10038 G:master env 
>>> ./manage.py test subscription 
Creating test database for alias 'default'... 
........ 
---------------------------------------------------------------------- 
Ran 8 tests in 0.012s 

OK 
Destroying test database for alias 'default'... 
+0

恐怕我在我的問題中省略了一個細節:我沒有包含我的應用程序的項目,也許我需要創建一個空的項目並添加我的可重用應用程序來運行測試? – Masci 2012-02-24 11:38:28

1

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

你得到這一點,因爲DJANGO_SETTINGS_MODULE是不是在你的Python環境變量...要解決你的問題,你必須把它定義爲

import os 
os.environ['DJANGO_SETTINGS_MODULE'] = '<django_application_root>.settings' 

你可以把它添加到您的根__init__.py文件...

相關問題