Neon branching with Django pytest

I want to use Neon’s branching feature to create a new branch (a copy of the main database branch on Neon) every time the test suite runs.

In the project I’m working on, we use pytest-django, PostgreSQL as our database, and Django as the backend.

Pytest allows specifying a live database for testing (pytest-django docs). I’ve been trying to dynamically create a branch and a compute endpoint using the Neon APIs (Neon API docs). My goal is to use the newly created HOST for the test run, but the tests always run in the test_[DEFAULT_DB_NAME].

Here’s what my conftest.py file looks like:

@pytest.fixture(scope='session')
def django_db_setup():

    logger.info("Creating new test db branch")

    neon = NeonAPI(api_key=os.environ.get('NEON_API_KEY'))

    project_id = "project_id"
    branch = neon.branch_create(project_id=project_id)

    settings.DATABASES['default'] = {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('PGDATABASE', ''),
        'USER': os.environ.get('PGUSER', ''),
        'PASSWORD': os.environ.get('PGPASSWORD', ''),
        'HOST': 'soemthing',
        'PORT': os.environ.get('PGPORT', '5432'),
    }

But this does not seem to have any effect at all.

Вернуться на верх