@@ -16,7 +16,7 @@ on what marks are and for notes on using_ them.
16
16
``pytest.mark.django_db `` - request database access
17
17
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
18
19
- .. py :function :: pytest.mark.django_db([transaction=False ])
19
+ .. py :function :: pytest.mark.django_db([transaction=False , serialized_rollback= False ])
20
20
21
21
This is used to mark a test function as requiring the database. It
22
22
will ensure the database is setup correctly for the test. Each test
@@ -38,6 +38,14 @@ on what marks are and for notes on using_ them.
38
38
uses. When ``transaction=True ``, the behavior will be the same as
39
39
`django.test.TransactionTestCase `_
40
40
41
+ :type serialized_rollback: bool
42
+ :param serialized_rollback:
43
+ The ``serialized_rollback `` argument enables `rollback emulation `_.
44
+ After a `django.test.TransactionTestCase `_ runs, the database is
45
+ flushed, destroying data created in data migrations. This is the
46
+ default behavior of Django. Setting ``serialized_rollback=True ``
47
+ tells Django to restore that data.
48
+
41
49
.. note ::
42
50
43
51
If you want access to the Django database *inside a fixture *
@@ -54,6 +62,7 @@ on what marks are and for notes on using_ them.
54
62
Test classes that subclass Python's ``unittest.TestCase `` need to have the
55
63
marker applied in order to access the database.
56
64
65
+ .. _rollback emulation : https://docs.djangoproject.com/en/stable/topics/testing/overview/#rollback-emulation
57
66
.. _django.test.TestCase : https://docs.djangoproject.com/en/dev/topics/testing/overview/#testcase
58
67
.. _django.test.TransactionTestCase : https://docs.djangoproject.com/en/dev/topics/testing/overview/#transactiontestcase
59
68
@@ -191,6 +200,16 @@ transaction support. This is only required for fixtures which need
191
200
database access themselves. A test function would normally use the
192
201
:py:func: `~pytest.mark.django_db ` mark to signal it needs the database.
193
202
203
+ ``serialized_rollback ``
204
+ ~~~~~~~~~~~~~~~~~~~~~~~
205
+
206
+ When the ``transactional_db `` fixture is enabled, this fixture can be
207
+ added to trigger `rollback emulation `_ and thus restores data created
208
+ in data migrations after each transaction test. This is only required
209
+ for fixtures which need to enforce this behavior. A test function
210
+ would use :py:func: `~pytest.mark.django_db(serialized_rollback=True) `
211
+ to request this behavior.
212
+
194
213
``live_server ``
195
214
~~~~~~~~~~~~~~~
196
215
@@ -200,6 +219,12 @@ or by requesting it's string value: ``unicode(live_server)``. You can
200
219
also directly concatenate a string to form a URL: ``live_server +
201
220
'/foo ``.
202
221
222
+ Since the live server and the tests run in different threads, they
223
+ cannot share a database transaction. For this reason, ``live_server ``
224
+ depends on the ``transactional_db `` fixture. If tests depend on data
225
+ created in data migrations, you should add the ``serialized_rollback ``
226
+ fixture.
227
+
203
228
``settings ``
204
229
~~~~~~~~~~~~
205
230
0 commit comments