繁体   English   中英

使用标准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 )也是如此。

我怎样才能解决这个问题?

ps这不是该问题的重复项。 它的作者在进行标准Django测试时遇到了问题,尽管它在我的项目中正常工作。 我的麻烦是第三方测试。

不幸的是,您所做的还不足以初始化django的设置系统。 您可以尝试以下方法:

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

setup_environ(settings)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM