[Django]-How can I load all fixtures inside particular directory while writing TestCases in Django using Pytest

3👍

You can use glob from python which is used to return all file paths that match a specific pattern

import glob

@pytest.fixture(scope="session")
def load_admin_data(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata',glob.glob('fixtures_for_tests/basic_fixtures/*'))

Also if you want to add more folders, you can do it by

import glob

@pytest.fixture(scope="session")
def django_db_setup1(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata',
                     glob.glob('fixtures_for_tests/basic_fixtures/*'), glob.glob('fixtures_for_tests/admins/*'))

Leave a comment