gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:20 27 4
gpt4 key购买 nike

有人知道如何使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com