当DATABASES'默认'时,Django无法运行测试。是空的

时间:2014-12-29 09:55:33

标签: python django

设置DATABASES['default'] = {}时,我遇到了一些运行测试的麻烦。

运行./manage.py test

时出现了以下错误
$ ./manage.py test --failfast
Creating test database for alias 'mydb'...
E
======================================================================
ERROR: test_admin (myapp.test.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
    self._pre_setup()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 754, in _pre_setup
    self._fixture_setup()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 887, in _fixture_setup
    if not connections_support_transactions():
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 874, in connections_support_transactions
    for conn in connections.all())
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 874, in <genexpr>
    for conn in connections.all())
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 782, in supports_transactions
    self.connection.leave_transaction_management()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 338, in leave_transaction_management
    if managed == self.get_autocommit():
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 345, in get_autocommit
    self.ensure_connection()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/home/thomas/.virtualenvs/tmpapp/local/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'

----------------------------------------------------------------------
Ran 0 tests in 0.001s

FAILED (errors=1)
Destroying test database for alias 'mydb'...

settings.py

..
DATABASES = {
    'default': {},
    'mydb': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}
DATABASE_ROUTERS = ['myapp.router.MyRouter']

router.py

class MyRouter:
    def db_for_read(self, model, **hints):
        return 'mydb'

    def db_for_write(self, model, **hints):
        return 'mydb'

    def allow_migrate(self, db, model):
        if db == 'default':
            return False
        return True

test.py

from django.test import TestCase                                                                                                                                       


class MyTest(TestCase):

    def test_admin(self):
        self.assertEqual(1, 1)

我做了一些调查,发现self.connect()在与'default'别名关联的虚拟后端上被调用。由于虚拟后端没有实现太多的功能,因此调用失败。如何使用DATABASES['default'] = {}运行测试?

1 个答案:

答案 0 :(得分:0)

使用不同的settings.py文件进行生产/开发/测试(最好)。

或者使用Sqlite作为默认条目(最快)。