我使用django作为后端,使用pytest来处理我的测试。
我把我的项目数据库从sqlite3切换到postgres,除了测试之外,一切都很好,出于某种原因,我的所有测试都失败了。
在切换之前,我可以在测试期间使用以下行访问我的数据库:
pytestmark = pytest.mark.django_db
但现在,在使用Postgres作为数据库后,我在所有测试中都出现了这个错误
UndefinedTable
The above exception was the direct cause of the following exception:
request = <SubRequest '_django_db_marker' for <Function test_logged_user_saved_recipes_url_reverse>>
@pytest.fixture(autouse=True)
def _django_db_marker(request):
"""Implement the django_db marker, internal to pytest-django.
This will dynamically request the ``db``, ``transactional_db`` or
``django_db_reset_sequences`` fixtures as required by the django_db marker.
"""
marker = request.node.get_closest_marker("django_db")
if marker:
transaction, reset_sequences = validate_django_db(marker)
if reset_sequences:
request.getfixturevalue("django_db_reset_sequences")
elif transaction:
request.getfixturevalue("transactional_db")
else:
> request.getfixturevalue("db")
有人知道在使用pytest时访问Postgres数据库的正确方法吗?或者有一种方法可以只在测试时切换到sqlite3db?
提前感谢
因此将此代码添加到conftest.py中为我解决了问题
@pytest.fixture(scope='session')
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgres',
'HOST': 'localhost',
'NAME': 'postgres',
}