Skip to content

Warn when --reuse-db is used with an in-memory sqlite database - #1310

Open
ChrisJr404 wants to merge 2 commits into
pytest-dev:mainfrom
ChrisJr404:warn-reuse-db-in-memory
Open

Warn when --reuse-db is used with an in-memory sqlite database#1310
ChrisJr404 wants to merge 2 commits into
pytest-dev:mainfrom
ChrisJr404:warn-reuse-db-in-memory

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #160.

Using --reuse-db with an in-memory sqlite database is a no-op: the database only lives for the duration of the process, so there's nothing to reuse or keep between runs. This trips people up (the default sqlite test database is in-memory), which is what the issue describes, and both Andreas and Daniel agreed a warning would be the right nudge.

So this emits a PytestWarning from django_db_setup when --reuse-db is requested (and --create-db isn't) and any of the databases being set up is an in-memory sqlite one. It only fires when you actually pass --reuse-db, so default runs are unaffected. Uses the same vendor == "sqlite" and is_in_memory_db() check already used for the live server.

Added tests for both the warning firing and staying quiet without the flag, plus a changelog line and a short note in the database docs.

An in-memory sqlite database only exists for the lifetime of the process,
so --reuse-db has nothing to reuse or keep. Emit a warning in that case to
avoid the confusion reported in pytest-dev#160.
Comment thread pytest_django/fixtures.py
Comment thread pytest_django/fixtures.py Outdated
Comment thread pytest_django/fixtures.py Outdated
@ChrisJr404

Copy link
Copy Markdown
Author

Renamed the variable to in_memory_aliases as suggested.

On the repr: yes, the aliases are always strings, so it will not fail. I kept repr deliberately so the alias names come out quoted in the warning (e.g. 'default'), which reads more clearly than bare names when several are listed. Happy to drop it to a plain join if you'd prefer the unquoted form.

@kingbuzzman
kingbuzzman requested a review from bluetech August 31, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raise warning when using SQLite3 in-memory test database with --reuse-db

2 participants