Skip to content

Potential documentation error #541

@callmesangio

Description

@callmesangio

Hi, is this note still true?

If you want access to the Django database *inside a fixture*
this marker will not help even if the function requesting your
fixture has this marker applied. To access the database in a
fixture, the fixture itself will have to request the ``db`` or
``transactional_db`` fixture. See below for a description of
them.

This works for me:

import pytest
from django.contrib.auth.models import User

@pytest.fixture
def dbusers():
    return User.objects.count()

@pytest.mark.django_db
def test_usercount(dbusers):
    assert dbusers == 0

Thanks for your work on pytest-django!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions