使用标准 Unittest 测试 Django:'DatabaseWrapper' 对象没有属性 'Database'

testing Django with standard Unittest: 'DatabaseWrapper' object has no attribute 'Database'

我正在尝试单独测试我的 Django 测试文件,一个一个地测试,因为 ./manage.py test 由于繁重的应用程序在每个 运行 后冻结 5 秒。

这是我的测试(尽管还不是测试,只是在玩请求):

if __name__ == "__main__":
    import unittest

    # manually get all the django stuff into memory if file is called directly
    import os,sys
    TEST_ROOT = os.path.realpath(os.path.dirname(__file__))
    PROJ_AND_TEST_ROOT = os.path.dirname(os.path.dirname(TEST_ROOT))
    PROJECT_ROOT = os.path.join(PROJ_AND_TEST_ROOT, 'the_game')
    sys.path.append(PROJECT_ROOT)
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "the_game.settings")
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()


from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User

from interface.views import bets

class ViewTests(TestCase):
    def test_bets_view(self):
        request_factory=RequestFactory()
        request=request_factory.get('/')

        user = User.objects.create_user('testname','test@na.me','testname')
        request.user=user
        response=bets(request)
        print response


if __name__ == "__main__":
    unittest.main()

但是,运行我得到以下信息:

======================================================================
ERROR: test_bets_view (__main__.ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
    self._pre_setup()
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 754, in _pre_setup
    self._fixture_setup()
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 887, in _fixture_setup
    if not connections_support_transactions():
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 874, in connections_support_transactions
    for conn in connections.all())
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 874, in <genexpr>
    for conn in connections.all())
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 782, in supports_transactions
    self.connection.leave_transaction_management()
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 338, in leave_transaction_management
    if managed == self.get_autocommit():
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 345, in get_autocommit
    self.ensure_connection()
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/utils.py", line 86, in __exit__
    db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
AttributeError: 'DatabaseWrapper' object has no attribute 'Database'

项目本身运行完美,没有错误 (./manage.py runserver),django 测试也是如此 (./manage.py test ../tests)。

我该如何解决这个问题?

p.s。这不是 this question 的副本。它的作者在标准 Django 测试方面遇到了问题,但它对我的项目来说运行良好。我的麻烦是第三方测试。

不幸的是,您所做的还不足以初始化 Django 的设置系统。你可以试试这个:

from django.core.management import setup_environ
from mysite import settings

setup_environ(settings)

或者您可以考虑编写调用测试运行程序的自定义管理命令(这将已经初始化设置系统)。