Skip to content

Instantly share code, notes, and snippets.

@marksweb
Created August 2, 2025 10:01
Show Gist options
  • Select an option

  • Save marksweb/5f17f1b12dc829b6ebbb501620011543 to your computer and use it in GitHub Desktop.

Select an option

Save marksweb/5f17f1b12dc829b6ebbb501620011543 to your computer and use it in GitHub Desktop.
Models as pytest fixtures
from django.apps import apps
def create_model_fixture(model):
"""
This injects the model names as fixtures.
eg def test_something(MyModel):
will make the `MyModel` available in a test.
"""
def _fixture():
return model
_fixture.__name__ = model._meta.object_name
return _fixture
for models in apps.all_models.values():
for model in models.values():
vars()[model._meta.object_name] = pytest.fixture(scope="session")(
create_model_fixture(model)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment