Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Compatibility

* Official Django 6.1 support.

Improvements
^^^^^^^^^^^^

* Warn when ``--reuse-db`` is used with an in-memory SQLite database, since
such a database only lives for the duration of the process and can't be
reused between test runs.

v4.14.0 (2026-08-10)
--------------------

Expand Down
5 changes: 5 additions & 0 deletions docs/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ instantly be re used. This will allow much faster startup time for tests.
This can be especially useful when running a few tests, when there are a lot
of database tables to set up.

Note that ``--reuse-db`` has no effect for an in-memory SQLite database (the
default for SQLite): such a database only exists while the process is running,
so there is nothing to reuse between runs. pytest-django emits a warning in
this case.

``--reuse-db`` will not pick up schema changes between test runs. You must run
the tests with ``--reuse-db --create-db`` to re-create the database according
to the new schema. Running without ``--reuse-db`` is also possible, since the
Expand Down
18 changes: 18 additions & 0 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ def django_db_setup( # noqa: PLR0917
**setup_databases_args,
)

if django_db_keepdb and not django_db_createdb:
from django.db import connections

in_memory_aliases = sorted(
alias
for alias in aliases
if connections[alias].vendor == "sqlite" and connections[alias].is_in_memory_db()
Comment thread
kingbuzzman marked this conversation as resolved.
)
if in_memory_aliases:
request.node.warn(
pytest.PytestWarning(
"--reuse-db has no effect for the in-memory sqlite database(s) "
f"{', '.join(repr(alias) for alias in in_memory_aliases)}: an in-memory "
"database only exists for the duration of the process, so there "
"is nothing to reuse between test runs."
)
)

yield

if not django_db_keepdb:
Expand Down
43 changes: 43 additions & 0 deletions tests/test_db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,46 @@ class Migration(migrations.Migration):
assert result.ret == 0
result.stdout.fnmatch_lines(["*test_something_without_db PASSED*"])
result.stdout.no_fnmatch_line("*mark_migrations_run*")


class TestSqliteReuseDbInMemory:
db_settings: ClassVar = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}

def test_reuse_db_warns_for_in_memory_database(self, django_pytester: DjangoPytester) -> None:
"--reuse-db can't reuse an in-memory database, so warn about it."
django_pytester.create_test_module(
"""
import pytest

@pytest.mark.django_db
def test_inner():
pass
"""
)

result = django_pytester.runpytest_subprocess("-v", "--reuse-db")
assert result.ret == 0
result.stdout.fnmatch_lines(
["*--reuse-db has no effect for the in-memory sqlite database*"]
)

def test_no_warning_without_reuse_db(self, django_pytester: DjangoPytester) -> None:
"Without --reuse-db the warning must not be emitted."
django_pytester.create_test_module(
"""
import pytest

@pytest.mark.django_db
def test_inner():
pass
"""
)

result = django_pytester.runpytest_subprocess("-v")
assert result.ret == 0
result.stdout.no_fnmatch_line("*--reuse-db has no effect*")