Skip to content

Commit c35b0c2

Browse files
committed
Document how data can be loaded once into the test database.
Refs pytest-dev#105, pytest-dev#243.
1 parent 3b0d917 commit c35b0c2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: docs/database.rst

+26
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,29 @@ Put this into ``conftest.py``::
333333
'HOST': 'db.example.com',
334334
'NAME': 'external_db',
335335
}
336+
337+
338+
Populate the database with initial test data
339+
""""""""""""""""""""""""""""""""""""""""""""
340+
341+
This example shows how you can populate the test database with test data. The
342+
test data will be saved in the database, i.e. it will not just be part of a
343+
transactions. This example uses Django's fixture loading mechanism, but it can
344+
be replaced with any way of loading data into the database.
345+
346+
Notice that :fixture:`django_db_setup` is in the argument list. This may look
347+
odd at first, but it will make sure that the sure that the original
348+
pytest-django fixture is used to create the test database. When
349+
``call_command`` is invoked, the test database is already prepared and
350+
configured.
351+
352+
Put this in conftest.py::
353+
354+
import pytest
355+
356+
from django.core.management import call_command
357+
358+
@pytest.fixture(scope='session')
359+
def django_db_setup(django_db_setup, django_db_blocker):
360+
with django_db_blocker:
361+
call_command('loaddata', 'your_data_fixture.json')

0 commit comments

Comments
 (0)