【发布时间】:2022-06-11 21:23:10
【问题描述】:
我想使用专门的测试数据库对我的 Django 应用程序进行单元测试。我正在使用pytest 和pytest-django。根据pytest-django 说明,我在conftest.py 文件中提供了我自己的自定义django_db_setup 夹具,如下所示:
from pathlib import Path
import pytest
from django.conf import settings
@pytest.fixture(scope='session')
def django_db_setup():
base_dir = Path(__file__).parent
path = base_dir / 'test_db.sqlite3'
assert path.exists()
assert path.is_file()
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': path
}
我可以确认在运行单元测试时正在执行此代码,并且正在找到 test_db.sqlite3 数据库。但是,单元测试仍然指的是我的开发数据库而不是我的测试数据库。
如何让pytest-django 引用我的测试数据库而不是我的开发数据库?
【问题讨论】:
标签: python django pytest pytest-django