Skip to content

Commit

Permalink
Document how data can be loaded once into the test database.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelme committed Jul 3, 2016
1 parent 3b0d917 commit c35b0c2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,29 @@ Put this into ``conftest.py``::
'HOST': 'db.example.com',
'NAME': 'external_db',
}


Populate the database with initial test data
""""""""""""""""""""""""""""""""""""""""""""

This example shows how you can populate the test database with test data. The
test data will be saved in the database, i.e. it will not just be part of a
transactions. This example uses Django's fixture loading mechanism, but it can
be replaced with any way of loading data into the database.

Notice that :fixture:`django_db_setup` is in the argument list. This may look
odd at first, but it will make sure that the sure that the original
pytest-django fixture is used to create the test database. When
``call_command`` is invoked, the test database is already prepared and
configured.

Put this 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:
call_command('loaddata', 'your_data_fixture.json')

0 comments on commit c35b0c2

Please sign in to comment.