添加一个测试用例后,Django 测试无法安装夹具

Django tests failing to install a fixture after adding one more TestCase

我几乎开始怀疑 Django 应用程序可以拥有多少个 TestCases 是否有一些限制,所以我需要你的帮助来弄清楚如何避免这个问题并在我的应用程序中进行功能测试测试用例。

我的 Django (v1.10.7) 应用程序有 4 个 fixture TestCases,运行 没问题。一旦我添加了带有固定装置的第 5 个 TestCase,运行ning 测试 与

一起

python3 ./manage.py 测试 myapp

开始导致随机的一个旧测试用例失败:

ERROR: setUpClass (myapp.tests.RandomOldTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/django/db/backends/base/base.py", line 206, in _cursor
    return self.create_cursor()
  File "/usr/lib/python3/dist-packages/django/db/backends/postgresql/base.py", line 211, in create_cursor
    cursor = self.connection.cursor()
psycopg2.InterfaceError: connection already closed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/django/test/testcases.py", line 1019, in setUpClass
    'database': db_name,
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 130, in call_command
    return command.execute(*args, **defaults)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3/dist-packages/django/core/management/commands/loaddata.py", line 64, in handle
    self.loaddata(fixture_labels)
  File "/usr/lib/python3/dist-packages/django/core/management/commands/loaddata.py", line 104, in loaddata
    self.load_label(fixture_label)
  File "/usr/lib/python3/dist-packages/django/core/management/commands/loaddata.py", line 167, in load_label
    obj.save(using=self.using)
  File "/usr/lib/python3/dist-packages/django/core/serializers/base.py", line 201, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/usr/lib/python3/dist-packages/django/db/models/base.py", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/lib/python3/dist-packages/django/db/models/base.py", line 889, in _save_table
    forced_update)
  File "/usr/lib/python3/dist-packages/django/db/models/base.py", line 939, in _do_update
    return filtered._update(values) > 0
  File "/usr/lib/python3/dist-packages/django/db/models/query.py", line 654, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py", line 1148, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py", line 833, in execute_sql
    cursor = self.connection.cursor()
  File "/usr/lib/python3/dist-packages/django/db/backends/base/base.py", line 233, in cursor
    cursor = self.make_cursor(self._cursor())
  File "/usr/lib/python3/dist-packages/django/db/backends/base/base.py", line 206, in _cursor
    return self.create_cursor()
  File "/usr/lib/python3/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib/python3/dist-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3/dist-packages/django/db/backends/base/base.py", line 206, in _cursor
    return self.create_cursor()
  File "/usr/lib/python3/dist-packages/django/db/backends/postgresql/base.py", line 211, in create_cursor
    cursor = self.connection.cursor()
django.db.utils.InterfaceError: Problem installing fixture '/home/lynoure/PycharmProjects/usertools2/useraccounts/fixtures/test-fixtures.json': connection already closed

新的 TestCase 是什么似乎无关紧要,即使我用新名称复制预先存在的测试也会发生这种情况。

以防万一,这是在Debian Stable上,postgresql的版本是9.6+181,python3-psycopg2是2.6.2-1。

作为一种解决方法,可以标记一些测试用例,然后仅 运行 测试排除标记:

from django.test import tag

@tag('trouble')
class TroubleTestCase(TestCase):
...

然后运行:

python3 ./manage.py test myapp --exclude-tag=trouble

之后必须 运行 分别进行这些测试:

python3 ./manage.py test myapp --tag=trouble

事实证明,如果你有几个

super().tearDownClass()

您的代码中缺少,这就是您得到的那种症状。因此,如果您遇到这种情况,请检查您的 TestCase 是否具有调用父级的 tearDownClass。