pytest django load database data from yaml file

Viewed 519

I have dumped data from my development environment using dumpdata django command into a yaml file.

I am using pytest framework and need to achieve the following scenario:

  • During start of a pytest session -> that is when I run pytest from command line, the data from yaml file should be loaded into my test database

Is there any plugin or hook which I can use to achieve the above ?

I am using python 3.8, pytest 6.2.4, pytest-django 4.40, Django 3.0.5

1 Answers

If you are using the pytest-django package then you can load Django data fixtures for your tests by providing a django_db_setup fixture pytest fixture in conftest.py

import pytest

from django.core.management import call_command

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', 'my_fixture.yaml')
Related