Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest django load database data from yaml file

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

like image 712
Adhithyan Vijayakumar Avatar asked Aug 10 '26 22:08

Adhithyan Vijayakumar


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')
like image 125
Iain Shelvington Avatar answered Aug 12 '26 10:08

Iain Shelvington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!