简体   繁体   中英

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

I am trying to test my Django test files separately, one by one, as ./manage.py test freezes for 5secs after each run due to heavy apps.

This is my test (not even a test yet though, just playing with requests):

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()

However, running this I get the following:

======================================================================
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'

The project itself works perfectly without errors ( ./manage.py runserver ), as do django tests ( ./manage.py test ../tests ).

How can I fix this?

ps This is not a duplicate of this question . Its author had problems with standard Django testing, while it works fine for my project. My trouble is with third-party testing.

Unfortunately, what you're doing is not quite enough to initialize django's settings system. You can try this:

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

setup_environ(settings)

Or you could look into writing a custom managment command (which will have the settings system initialized already) which calls the test runner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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