运行 使用 pytest-django 对现有数据库进行测试

Running tests against existing database using pytest-django

有人知道如何 运行 使用 pytest-django 对现有(例如生产)数据库进行 Django 测试吗?
我知道一般来说,这不是单元测试应该做的,但就我而言,我 运行 在 Heroku 上进行测试。默认情况下,Django 会创建一个新的测试数据库,但是,这在 Heroku 上是不可能的。

我找到了一个没有 pytest-django 的解决方案(python manage.py test):https://gist.github.com/gregsadetsky/5018173
但据我所知,pytest-django 没有使用 Django 设置中定义的测试 运行ner。

如果有人有关于如何在 Heroku 上使用 pytest-django 运行 Django 测试的另一种方法(例如,通过自动化创建测试数据库的方法),我也会对那个解决方案感到满意。

关注 the documentation 如何 link 到现有数据库:

Using an existing, external database for tests

This example shows how you can connect to an existing database and use it for your tests.
This example is trivial, you just need to disable all of pytest-django and Django’s test database creation and point to the existing database. This is achieved by simply implementing a no-op django_db_setup fixture.

Put this into conftest.py:

import pytest


@pytest.fixture(scope='session')
def django_db_setup():
    settings.DATABASES['default'] = {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'db.example.com',
        'NAME': 'external_db',
    }