pytest-dev / pytest-dev/pytest-django

`--reuse-db` does nothing and test database is flushed

Open
#1,052 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

As per the docs:

Using --reuse-db will create the test database in the same way as manage.py test usually does. However, after the test run, the test database will not be removed. The next time a test run is started with --reuse-db, the database will instantly be re used. This will allow much faster startup time for tests.

This actually doesn't work

Here's pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings
addopts = --reuse-db

and here's django_db_setup

import sqlite3
import uuid

import pytest
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management import call_command


@pytest.fixture(scope='session')
def test_user():
    return {
        'username': uuid.uuid4().hex[:10],
        'password': uuid.uuid4().hex,
    }


@pytest.fixture(scope='session')
def django_db_setup(django_db_blocker, test_user, django_db_keepdb):
    db_path = settings.BASE_DIR / 'test-db.sqlite3'
    sqlite3.connect(db_path.as_posix())
    settings.DATABASES['default']['NAME'] = db_path
    with django_db_blocker.unblock():
        call_command('migrate', '--noinput')
        User.objects.create_user(
            username=test_user['username'],
            password=test_user['password'],
            email=f'{test_user["username"]}@test',
            is_active=True,
        )

and django_db_keepdb evaluates to True. Here's the tests:

def test1(django_user_model, django_db_keepdb):
    print(django_db_keepdb, django_user_model.objects.all())


def test2(django_user_model, django_db_keepdb):
    print(django_db_keepdb, django_user_model.objects.all())

which results in:

True <QuerySet [<User: b18cd8369b>]>
True <QuerySet []>

and the expected results are

True <QuerySet [<User: b18cd8369b>]>
True <QuerySet [<User: b18cd8369b>]>

So, how to actually obtain this outcome?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the pytest.ini configuration, the custom django_db_setup fixture, and the test1/test2 examples. Trace how django_db_keepdb and the database fixture interact during test teardown; done means the second test still sees the created User while the reuse-db behavior remains consistent with the linked documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python, sqlite
Domain
databases, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.