pytest-dev / pytest-dev/pytest-django

Running a test that uses live_server after a test that uses transactional_db causes database errors

Open
#1,197 2 comments 1 reaction 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

Hi all!

I am using python 3.13, sqlite 3.46 and pytest-django 4.8 on Debian Trixie. My test suite uses an sqlite in-memory database.

I am seeing an issue in my test suite where a test that uses playwright-async-pytest and live_server to test some frontend stuff will fail if it is run after a test that uses the transactional_db fixture. The test uses different custom fixtures to log into my Django App, create some DB objects and a playwright browser page that accesses an edit view.

The actual errors that the test fails with vary from run to run. They are all database related though, sometimes an object that should be there isn't, sometimes it exists twice, often there will be session related errors because session data cannot be retrieved from the session cache.

I've done some debugging and from what I can glean there are different connections used during the test's run time.

I've run the same test suite on Debian Bookworm and am not seeing the same issue there. I also see different connections though. My Bookworm suite has python 3.11 and sqlite 3.40, all other versions are the same as in Trixie.

My very vague theory is that the in-memory database get copied/cloned/forked/moved when the test using live_server is set up and there is something going wrong with that particular sqlite version, resulting in different queries ending up on different databases.

I'm afraid my Django-Fu wasn't quite up to digging deeper on this (I've tried, but lost my way in in the db backends), but I'm happy to try any further debugging measures people could recommend to figure this out.

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

Reproduce the failure by running a test using live_server after one using transactional_db with the reported Python, SQLite, Django, and pytest-django versions. Compare the database connections and session behavior on Debian Trixie and Bookworm, then trace the fixture and database setup involved. Done means identifying the cause and preventing the database-related failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python, sqlite
Domain
backend, database, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.