You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Running a test that uses live_server after a test that uses transactional_db causes database errors #1197
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.
first run a test that uses the transactional_db fixture
then run a test that uses the live_server fixture and fires a lot of request from the get-go (in my case an HTMX based view that gets assembled from several components)
Doing this will result in a corrupted database for the second test.
My hypothesis would be that something in the extra code that is run for in-memory databases fails with that version of sqlite, but I cannot say for sure. I've switched my tests to using a file-based database as a work-around.
When using this fixture what I've noticed is that database entries between tests don't get cleaned up, even if I'm not using any of the special options like --reuse-db which means that transactions aren't being rolled back correctly.
Unsure if it matters, but for playwright to run I have to set:
Activity
m-reichle commentedon May 9, 2025
I've narrowed this down to the following factors:
transactional_dbfixturelive_serverfixture and fires a lot of request from the get-go (in my case an HTMX based view that gets assembled from several components)Doing this will result in a corrupted database for the second test.
My hypothesis would be that something in the extra code that is run for in-memory databases fails with that version of sqlite, but I cannot say for sure. I've switched my tests to using a file-based database as a work-around.
suspiciousRaccoon commentedon Jul 21, 2025
I've found similar problems when using
live_serveralong with playwright'spagefixture.When using this fixture what I've noticed is that database entries between tests don't get cleaned up, even if I'm not using any of the special options like
--reuse-dbwhich means that transactions aren't being rolled back correctly.Unsure if it matters, but for playwright to run I have to set:
in my
conftest.py, even though I'm using the sync api and all my test are sync only.I found that in this stackoverflow question.
I imagine for async tests this would be worse and even more unpredictable.
If required I could set up a repo with a MCVE of the issue.