django pytest how to load data

# 1. Create a fixtures directory in your Django app (if not already present).
#    This is where you'll store your fixture files (e.g., initial_data.json).

# 2. Inside the fixtures directory, create a JSON file with the data you want to load.
#    For example, create a file named initial_data.json.

# 3. In your Django app, update the pytest.ini or pytest.cfg file to include the following:

[pytest]
DJANGO_SETTINGS_MODULE = your_project.settings

# 4. In your test file (e.g., test_your_module.py), use the pytest.fixture decorator
#    to define a fixture function that loads the data using Django's `django_db_setup`
#    and `django_db_blocker` fixtures.

import pytest
from django.core.management import call_command

@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
    pass

@pytest.fixture
def your_loaded_data(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', 'initial_data.json')

# 5. Use the fixture in your test functions by including it as an argument.
#    The data will be loaded into the database before the test function is executed.

def test_your_function(your_loaded_data):
    # Your test logic here, with the database populated by the loaded data.

Ensure that the pytest-django package is installed in your project (pip install pytest-django). This setup assumes that your Django project structure is standard and that you are using the default database. Adjust the file and function names as needed for your specific project structure and requirements.