Skip to content

Provide a context manager/fixture to disable DB access explicitly #372

@blueyed

Description

@blueyed

I think the following code looks weird ,because it reads like it would disable the DB.

@pytest.fixture
def myfixture(django_db_blocker):
    with django_db_blocker:
        ...  # modify something in the database

But that's a manager/fixture I would like to have.

What I came up with is this, meant to work with pytest-django master and 2.9.1:

@pytest.yield_fixture
def disable_db(request):
    from _pytest.python import FixtureLookupError
    try:
        manager = request.getfuncargvalue('django_db_blocker')
        manager.disable_database_access()
        yield
        manager.restore_previous_access()
    except FixtureLookupError:
        manager = request.getfuncargvalue('_django_cursor_wrapper')
        manager.disable()
        yield
        manager.restore()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions