Skip to content

Commit 192b634

Browse files
Michał Pasternakbluetech
authored andcommitted
docs: describe how to populate the database in case you use transaction and transaction-less database.
[ran: adjusted]
1 parent 93a96f6 commit 192b634

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

docs/database.rst

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,17 @@ Put this into ``conftest.py``::
373373
Populate the database with initial test data
374374
""""""""""""""""""""""""""""""""""""""""""""
375375

376-
This example shows how you can populate the test database with test data. The
377-
test data will be saved in the database, i.e. it will not just be part of a
378-
transactions. This example uses Django's fixture loading mechanism, but it can
379-
be replaced with any way of loading data into the database.
376+
In some cases you want to populate the test database before you start the
377+
tests. Because of different ways you may use the test database, there are
378+
different ways to populate it.
380379

381-
Notice that :fixture:`django_db_setup` is in the argument list. This may look
382-
odd at first, but it will make sure that the original pytest-django fixture
383-
is used to create the test database. When ``call_command`` is invoked, the
384-
test database is already prepared and configured.
380+
Populate the test database if you don't use transactional or live_server
381+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
385382

386-
Put this in ``conftest.py``::
383+
If you are using the :func:`pytest.mark.django_db` marker or :fixture:`db`
384+
fixture, you probably don't want to explictly handle transactions in your
385+
tests. In this case, it is sufficient to populate your database only
386+
once. You can put code like this in ``conftest.py``::
387387

388388
import pytest
389389

@@ -392,7 +392,43 @@ Put this in ``conftest.py``::
392392
@pytest.fixture(scope='session')
393393
def django_db_setup(django_db_setup, django_db_blocker):
394394
with django_db_blocker.unblock():
395-
call_command('loaddata', 'your_data_fixture.json')
395+
call_command('loaddata', 'my_fixture.json')
396+
397+
This loads the Django fixture ``my_fixture.json`` once for the entire test
398+
session. This data will be available to tests marked with the
399+
:func:`pytest.mark.django_db` mark, or tests which use the :fixture:`db`
400+
fixture. The test data will be saved in the database and will not be reset.
401+
This example uses Django's fixture loading mechanism, but it can be replaced
402+
with any way of loading data into the database.
403+
404+
Notice :fixture:`django_db_setup` in the argument list. This triggers the
405+
original pytest-django fixture to create the test database, so that when
406+
``call_command`` is invoked, the test database is already prepared and
407+
configured.
408+
409+
Populate the test database if you use transactional or live_server
410+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
411+
412+
In case you use transactional tests (you use the :func:`pytest.mark.django_db`
413+
marker with ``transaction=True``, or the :fixture:`transactional_db` fixture),
414+
you need to repopulate your database every time a test starts, because the
415+
database is cleared between tests.
416+
417+
The :fixture:`live_server` fixture uses :fixture:`transactional_db`, so you
418+
also need to populate the test database this way when using it.
419+
420+
You can put this code into ``conftest.py``. Note that while it it is similar to
421+
the previous one, the scope is changed from ``session`` to ``function``::
422+
423+
import pytest
424+
425+
from myapp.models import Widget
426+
427+
@pytest.fixture(scope='function')
428+
def django_db_setup(django_db_setup, django_db_blocker):
429+
with django_db_blocker.unblock():
430+
Widget.objects.create(...)
431+
396432

397433
Use the same database for all xdist processes
398434
"""""""""""""""""""""""""""""""""""""""""""""

0 commit comments

Comments
 (0)