pytest-dev / pytest-dev/pytest-django

`django_db_serialized_rollback` does not reset to previous db state

Open
#1,062 5 comments 4 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

Problem description

Ever since I added the live_server to my tests, other tests started to fail, that did previously pass.

Details

The fixture to setup my browser with live_server support:

@pytest.fixture
def live_browser(browser, live_server, django_db_serialized_rollback) -> WebDriver:
    return browser

Example of how I use that fixture:

def test_some_case(live_browser):
    pass

The test that fails when (and after) using the live_server fixture:

def test_user_get_absolute_url(user: User):
    assert user.get_absolute_url() == escape_uri_path(
        f"http://mydomain.com/users/{user.id}"
    )

Context infos:
I am using --reuse-db as part of my pytest.ini file.

Problem Analysis

I understand this happens due to improper reset (flush) of the test database after any test using the live_server fixture.

I did some research and found this issue: https://github.com/pytest-dev/pytest-django/issues/329

This lead me back to the docs of pytest-django where I payed closer attention and found django_db_serialized_rollback: https://pytest-django.readthedocs.io/en/latest/helpers.html#std-fixture-django_db_serialized_rollback
As I understand it, this would in theory restore the content of the test database after a test that uses my fixture.

However this does either not seem to be the case, or not work as expected.

The user.get_absolute_url() uses the django.contrib Site model, for which I have a data migration.
The content seems to get flushed in the tests using live_server and never get restored.
Running only the broken test again after running my full test suit, still fails until I recreate the test database manually.

Solution ideas / requests

Workaround: Removing --reuse-db.
Sadly this is the only reliable way I found so far to get my test to work again.
And even then I need to run pytest twice once excluding the tests using live_server and then running them separately.
Obviously this is considerably slowing down the (TDD) testing process.

At this point I don't really have any proper solution ideas.
I would be happy to clarify any misunderstandings I have,
and maybe even discuss potential solution ideas
(or at least better workarounds). 😊

If I can provide further context to clarify the issue and use case, I'll gladly provide it.
Looking forward to hearing some feedback. 😅

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 interaction between the live_server and django_db_serialized_rollback fixtures with --reuse-db, using the examples in the issue. Inspect the fixture behavior and the linked pytest issue and documentation; done means database state, including migrated Site data, is restored after live_server tests and subsequent tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
databases, 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.