pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.32k stars 341 forks source link

Fixture django_db_setup stopped working when upgrading from 4.6 to 4.7 #1115

Open jeroenbrouwer opened 3 months ago

jeroenbrouwer commented 3 months ago

I've been using a custom django_db_setup fixture which has worked for years. After upgrading from 4.6 to 4.7 this suddenly doesn't work anymore, resulting in the following error:

RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

This might be a bug. Anybody any idea how to fix this from 4.7? The fixture is something like this:

@pytest.yield_fixture(scope="session", autouse=True)
def django_db_setup(django_db_blocker):
    with django_db_blocker.unblock():
        # Code that requires DB access and populates data required for all tests.
        Model.objects.create()

        # At this point the DB setup for the tests is complete
        yield
dperetti commented 3 months ago

Shouldn't your yield be aligned with the with?

@pytest.yield_fixture(scope="session", autouse=True)
def django_db_setup(django_db_blocker):
    with django_db_blocker.unblock():
        # Code that requires DB access and populates data required for all tests.
        Model.objects.create()

    # At this point the DB setup for the tests is complete
    yield
jeroenbrouwer commented 3 months ago

Thanks for the suggestion, it doesn't solve it unfortunately.

jeroenbrouwer commented 3 months ago

Somehow it always worked without marking the individual tests with pytest.mark.django_db until now.... Fixed for me, so I'm closing this issue.

jeroenbrouwer commented 3 months ago

Reopening because the problem still occurs when running this in my CI pipeline. Downgrading to 4.6 fixes it.

yhay81 commented 2 months ago

After updating pytest-django from v4.4 to v4.8, I'm experiencing the same database access error despite using @pytest.mark.django_db. This issue(https://github.com/pytest-dev/pytest-django/issues/1118) also seems to be the same one.