@@ -13,8 +13,8 @@ on what marks are and for notes on using_ them.
13
13
.. _using : https://pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
14
14
15
15
16
- ``pytest.mark.django_db(transaction=False) `` - request database access
17
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16
+ ``pytest.mark.django_db(transaction=False, serialized_rollback=False ) `` - request database access
17
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
18
19
19
.. :py:function:: pytest.mark.django_db:
20
20
@@ -38,6 +38,14 @@ when trying to access the database.
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 @@ when trying to access the database.
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
@@ -194,6 +203,16 @@ transaction support. This is only required for fixtures which need
194
203
database access themselves. A test function would normally use the
195
204
:py:func: `~pytest.mark.django_db ` mark to signal it needs the database.
196
205
206
+ ``serialized_rollback ``
207
+ ~~~~~~~~~~~~~~~~~~~~~~~
208
+
209
+ When the ``transactional_db `` fixture is enabled, this fixture can be
210
+ added to trigger `rollback emulation `_ and thus restores data created
211
+ in data migrations after each transaction test. This is only required
212
+ for fixtures which need to enforce this behavior. A test function
213
+ would use :py:func: `~pytest.mark.django_db(serialized_rollback=True) `
214
+ to request this behavior.
215
+
197
216
``live_server ``
198
217
~~~~~~~~~~~~~~~
199
218
@@ -203,6 +222,12 @@ or by requesting it's string value: ``unicode(live_server)``. You can
203
222
also directly concatenate a string to form a URL: ``live_server +
204
223
'/foo ``.
205
224
225
+ Since the live server and the tests run in different threads, they
226
+ cannot share a database transaction. For this reason, ``live_server ``
227
+ depends on the ``transactional_db `` fixture. If tests depend on data
228
+ created in data migrations, you should add the ``serialized_rollback ``
229
+ fixture.
230
+
206
231
``settings ``
207
232
~~~~~~~~~~~~
208
233
0 commit comments