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

标签 python django postgresql heroku pytest

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

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

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

最佳答案

正在关注 the documentation关于如何链接到现有数据库:

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',
    }

关于python - 使用 pytest-django 对现有数据库运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48096745/

相关文章:

python - 在python OOP中如何打印具有属性的对象列表并添加每个产品的价格总和

python - 在文本或 json 文件中创建用户并使用它来登录 python web

python - IOError : request data read error

json - Postgres 以高效方式访问 JSONB 元素

sql - 有效计算当前活跃用户数

postgresql - postgres SELECT FOR UPDATE 中的锁定顺序

python - 使用 django 自定义管理器方法作为 Q 对象链的一部分

Python 3 : Sending files through socket.(客户端-服务器程序)

python - 如何访问 Scrapy CrawlSpider 中的特定 start_url?

python - 在 Django 中,如何查看用户的所有属性?