的Python 2.7 的Django 1.2Django的测试
我正奇怪LOCAL_SETTINGS行为,当我测试一个Django应用程序。我有我的<project>/settings.py
设置是这样的:
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (
("Me", "[email protected]"),
)
MANAGERS = ADMINS + (('Person1', '[email protected]'),)
# ... rest of settings
try:
from local_settings import *
except ImportError:
pass
和<project>/local_settings.py
我:
DEBUG = True
MANAGERS = (
('Me', '[email protected]'),
)
所以,虽然在本地工作,该MANAGERS
设置应(('Me', '[email protected]'),)
和DEBUG
应设置为True
。
但是,在测试其中一个应用程序时,我正在测试settings.DEBUG
并获取False
,但MANAGERS
设置已正确设置(它只有'我')。任何想法为什么会发生?以下是<project>/<app>/tests.py
文件的相关部分:
from django.conf import settings
from django.test import TestCase
# ...
class MyTests(TestCase):
def mytest(self):
if settings.DEBUG:
self.assertEqual(settings.MANAGERS, (('Me', '[email protected]'),))
else:
self.assertEqual(settings.MANAGERS, (('Me', '[email protected]'), ('Person1', '[email protected]')))
结果是
AssertionError: (('Me', '[email protected]'),) != (('Me', '[email protected]'), ('Person1', '[email protected]'))
所以它看起来是测试else
分支由于settings.DEBUG
设置不正确,然后提高AssertionError
因为settings.MANAGERS
设置正确。
如果我跑python manage.py shell
我得到这个:
>>> from django.conf import settings
>>> settings.DEBUG
True
>>> settings.MANAGERS
(('Me', '[email protected]'),)
所以他们正确设置在那里。
我知道我可以在我的测试中手动覆盖设置,但我想尝试使用settings.DEBUG
,以便无论测试是在本地运行还是在生产中都能通过。
任何想法?
的可能的复制[?如何进行单元测试与Django的不同设置(http://stackoverflow.com/questions/913549/how-to-unit-test-with-different-settings-in-django) –