'how to set up postgres database with pytest-django?
I would like pytest-django to install a Postgres extension when it creates a test database. I've been mucking with conftest.py to try to get this to work, but I'm stuck.
My conftest.py is at the top level of my project (same directory as manage.py), and contains:
from django.db import connection
import pytest_django
@pytest.fixture(scope='session')
def django_db_setup(*args, **kwargs):
pytest_django.fixtures.django_db_setup(*args, **kwargs)
cursor = connection.cursor()
cursor.execute("create extension pg_trgm")
But when I run it, I get:
_pytest.vendored_packages.pluggy.PluginValidationError: unknown hook 'pytest_django' in plugin <module 'conftest' from '/path/to/my/conftest.py'>
Solution 1:[1]
You could use the pre_migrate
signals. For example:
from django.db.models.signals import pre_migrate
from django.apps import apps
def app_pre_migration(sender, app_config, **kwargs):
cur = connection.cursor()
cur.execute('CREATE EXTENSION IF NOT EXISTS pg_trgm;')
pre_migrate.connect(app_pre_migration, sender=apps.get_app_config('app'))
I hope this could help you.
Solution 2:[2]
I've been trying to accomplish the same thing, and got this working (in conftest.py
):
@pytest.fixture(scope="session")
def django_db_setup(django_db_setup, django_db_blocker):
"""Test session DB setup."""
with django_db_blocker.unblock():
with connection.cursor() as cursor:
cursor.execute("CREATE EXTENSION IF NOT EXISTS citext;")
The key here is that you have to name your custom fixture with the same name as the standard pytest-django
fixture, but also include the default fixture as an argument. This is the way to run the standard fixture, rather than importing and calling it. I also found this failed unless I ran django_db_blocker.unblock()
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lefterisnik |
Solution 2 | bjudson |