这是Django文档中的示例测试。
from django.test import RequestFactory, TestCase
class SimpleTest(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_hello(self):
request = self.factory.get('/hello')
运行测试会引发以下错误:
Failure
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/suite.py", line 146, in _handleClassSetUp
setUpClass()
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/test/testcases.py", line 1026, in setUpClass
if not connections_support_transactions():
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/test/testcases.py", line 991, in connections_support_transactions
for conn in connections.all())
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/db/utils.py", line 226, in all
return [self[alias] for alias in self]
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/db/utils.py", line 223, in __iter__
return iter(self.databases)
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/db/utils.py", line 156, in databases
self._databases = settings.DATABASES
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/myuser/dev/pythonagent-my_package/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 39, 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.conf.settings.configure()
并没有任何改变。