Python 使用django.test.client测试应用程序http请求,获取错误

Python 使用django.test.client测试应用程序http请求,获取错误,python,django,unit-testing,Python,Django,Unit Testing,我们开始为我们的API(使用Django Rest框架创建)编写单元测试。我们决定从简单开始,使用内置的unittest和django.test.client类。我已经编写了一个测试的存根,它运行得很好: #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import import unittest # from django.test.client import Client clas

我们开始为我们的API(使用Django Rest框架创建)编写单元测试。我们决定从简单开始,使用内置的unittest和django.test.client类。我已经编写了一个测试的存根,它运行得很好:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import unittest

# from django.test.client import Client

class TestGrowth(unittest.TestCase):
    def setUp(self):
        # self.client = Client()
        pass

    def test_numbers_3_4(self):
        self.assertEqual(4, 4, True)

    def test_strings_a_3(self):
        self.assertEqual('andy', 'andy', True)


def suite():
    suite = unittest.TestSuite()

    # Add test cases to suite
    suite.addTests(unittest.makeSuite(TestGrowth))

    return suite

if __name__ == '__main__':
    # Run test suite
    unittest.TextTestRunner(verbosity=2).run(suite())
但是,只要我取消注释从django.test.client import client读取的
行,我就会得到一个错误:

Traceback (most recent call last):
  File "./AccountGrowth.py", line 8, in <module>
    from django.test.client import Client
  File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module>
    from django.test.client import Client, RequestFactory
  File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/client.py", line 21, in <module>
    from django.db import close_connection
  File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.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.
我使用下面的命令
python./AccountGrowth.py
从vagrant shell内部运行测试


有人能解释一下为什么会发生这种情况吗?

您没有通过Django的测试运行程序运行测试,它为您设置了所有这些。如果
块或它对
run()
的显式调用是
,那么您不需要
,并且您应该通过
/manage.py test

Daniel运行您的测试……目标是将它们作为大型套件的一部分运行,但我刚刚开始单元测试。当我尝试使用
/manage.py test
从项目的根目录运行它时,它的行为似乎不一样。
if "DATABASE_URL" in os.environ:
    # Parse database configuration from $DATABASE_URL
    DATABASES = {
        'default': dj_database_url.config()
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',  # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'emmasocial',                  # Or path to database file if using sqlite3.
            'USER': 'root',                        # Not used with sqlite3.
            'PASSWORD': '',                        # Not used with sqlite3.
            'HOST': '',                            # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                            # Set to empty string for default. Not used with sqlite3.
        }
    }