1

我在虚拟环境中有一个设置:

一切正常,唯一的问题是在运行测试期间。每次 django 在运行测试函数后尝试刷新数据库时都会抛出错误:

Traceback (most recent call last):
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 187, in __call__
    self._post_teardown()
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 796, in _post_teardown
    self._fixture_teardown()
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 889, in _fixture_teardown
    return super(TestCase, self)._fixture_teardown()
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 817, in _fixture_teardown
    inhibit_post_syncdb=self.available_apps is not None)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 79, in handle_noargs
    six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 67, in handle_noargs
    savepoint=connection.features.can_rollback_ddl):
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/db/transaction.py", line 251, in __enter__
    "The outermost 'atomic' block cannot use "
CommandError: Database test_dev_db couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: The outermost 'atomic' block cannot use savepoint = False when autocommit is off.

所以对于每个测试用例,我都有通过或失败,就像它假设的那样,但我也得到了这个烦人的错误。

我确实运行了django-admin.py sqlflush --settings=dev_settings --pythonpath=.,它很好地刷新了我的开发数据库,​​没有错误。

在几个测试函数中,我检查了从数据库中提取的几个模型,它似乎可以很好地刷新和重新创建对象,所以这就是它不会影响实际测试用例的原因。

我浏览了整个回溯,我有点理解它为什么会发生,但我不知道如何处理。任何帮助表示赞赏。

编辑

刚刚尝试使用 Django-nonrel-1.5 运行测试,没有问题。这似乎是 1.6 版本中的一个错误。

4

1 回答 1

1

使用SimpleTestCase或定制TestCase

class CustomTestCase(TestCase):

    def _fixture_teardown(self):
        for db_name in self._databases_names(include_mirrors=False):
            call_command('custom_flush', verbosity=0, interactive=False,
                         database=db_name, skip_validation=True,
                         reset_sequences=False,
                         allow_cascade=self.available_apps is not None,
                         inhibit_post_syncdb=self.available_apps is not None)

由于问题出transaction.atomic在命令中flush,您可能必须自己编写flush

于 2015-06-04T05:17:31.787 回答