Created
August 2, 2025 10:01
-
-
Save marksweb/5f17f1b12dc829b6ebbb501620011543 to your computer and use it in GitHub Desktop.
Models as pytest fixtures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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