Django测试不使用测试数据库

时间:2019-11-06 16:04:11

标签: django docker-compose django-tests

我正在尝试在django应用上运行一些测试,但是我在迁移过程中始终遇到错误。

我正在使用docker-compose运行我的应用。我可以运行服务器,在dev db中添加数据,没有任何问题,但是当我尝试运行测试时,出现此错误:

python3 manage.py test -v 3
Creating test database for alias 'default' ('test_myapp')...
Operations to perform:
  Synchronize unmigrated apps: gis, messages, rest_framework, staticfiles
  Apply all migrations: admin, app, auth, authtoken, contenttypes, sessions, token_blacklist
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application authtoken
Running pre-migrate handlers for application token_blacklist
Running pre-migrate handlers for application app
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  No migrations to apply.

  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

Running post-migrate handlers for application admin
Running post-migrate handlers for application auth
Running post-migrate handlers for application contenttypes
Running post-migrate handlers for application sessions
Running post-migrate handlers for application authtoken
Running post-migrate handlers for application token_blacklist
Running post-migrate handlers for application app
System check identified no issues (0 silenced).

测试正在运行,但是它们正在使用dev db(我可以在测试中访问我的dev数据),并且如果没有这个错误,我一次不能运行多个测试文件:

test_str (app.tests.tests_models.test_entity.TestEntity) ... Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 223, in create_cursor
    cursor = self.connection.cursor()
psycopg2.InterfaceError: connection already closed

我已经尝试删除我的数据库,所有 pycache 文件夹以及所有迁移,重新运行makemigrations和迁移,但是错误仍然显示...

0 个答案:

没有答案
相关问题