@@ -32,10 +32,10 @@ Markers
32
32
of the test. This behavior is the same as Django's standard
33
33
:class: `~django.test.TestCase ` class.
34
34
35
- In order for a test to have access to the database it must either
36
- be marked using the `` django_db `` mark or request one of the `` db ` `,
37
- `` transactional_db `` or `` django_db_reset_sequences `` fixtures. Otherwise the
38
- test will fail when trying to access the database.
35
+ In order for a test to have access to the database it must either be marked
36
+ using the :func: ` ~pytest.mark. django_db ` mark or request one of the :fixture: ` db `,
37
+ :fixture: ` transactional_db ` or :fixture: ` django_db_reset_sequences ` fixtures.
38
+ Otherwise the test will fail when trying to access the database.
39
39
40
40
:type transaction: bool
41
41
:param transaction:
@@ -60,8 +60,9 @@ Markers
60
60
or may not help even if the function requesting your fixture has this marker
61
61
applied, depending on pytest's fixture execution order. To access the
62
62
database in a fixture, it is recommended that the fixture explicitly request
63
- one of the ``db ``, ``transactional_db `` or ``django_db_reset_sequences ``
64
- fixtures. See below for a description of them.
63
+ one of the :fixture: `db `, :fixture: `transactional_db ` or
64
+ :fixture: `django_db_reset_sequences ` fixtures. See below for a description of
65
+ them.
65
66
66
67
.. note :: Automatic usage with ``django.test.TestCase``.
67
68
@@ -115,6 +116,7 @@ pytest-django provides some pytest fixtures to provide dependencies for tests.
115
116
More information on fixtures is available in the :ref: `pytest documentation
116
117
<pytest:fixtures>`.
117
118
119
+ .. fixture :: rf
118
120
119
121
``rf `` - ``RequestFactory ``
120
122
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -133,6 +135,8 @@ Example
133
135
response = my_view(request)
134
136
assert response.status_code == 200
135
137
138
+ .. fixture :: client
139
+
136
140
``client `` - ``django.test.Client ``
137
141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138
142
@@ -164,6 +168,7 @@ To use `client` as an authenticated standard user, call its
164
168
response = client.get('/private')
165
169
assert response.content == 'Protected Area'
166
170
171
+ .. fixture :: admin_client
167
172
168
173
``admin_client `` - ``django.test.Client `` logged in as admin
169
174
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -179,8 +184,8 @@ Example
179
184
response = admin_client.get('/admin/')
180
185
assert response.status_code == 200
181
186
182
- Using the `admin_client ` fixture will cause the test to automatically be marked for database use (no need to specify the
183
- `` django_db ` ` mark).
187
+ Using the `admin_client ` fixture will cause the test to automatically be marked
188
+ for database use (no need to specify the :func: ` ~pytest.mark. django_db ` mark).
184
189
185
190
.. fixture :: admin_user
186
191
@@ -190,9 +195,10 @@ Using the `admin_client` fixture will cause the test to automatically be marked
190
195
An instance of a superuser, with username "admin" and password "password" (in
191
196
case there is no "admin" user yet).
192
197
193
- Using the `admin_user ` fixture will cause the test to automatically be marked for database use (no need to specify the
194
- `` django_db ` ` mark).
198
+ Using the `admin_user ` fixture will cause the test to automatically be marked
199
+ for database use (no need to specify the :func: ` ~pytest.mark. django_db ` mark).
195
200
201
+ .. fixture :: django_user_model
196
202
197
203
``django_user_model ``
198
204
~~~~~~~~~~~~~~~~~~~~~
@@ -210,6 +216,7 @@ Example
210
216
def test_new_user(django_user_model):
211
217
django_user_model.objects.create(username="someone", password="something")
212
218
219
+ .. fixture :: django_username_field
213
220
214
221
``django_username_field ``
215
222
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -219,37 +226,43 @@ resolves to the user model's :attr:`~django.contrib.auth.models.CustomUser.USERN
219
226
Use this fixture to make pluggable apps testable regardless what the username field
220
227
is configured to be in the containing Django project.
221
228
229
+ .. fixture :: db
230
+
222
231
``db ``
223
232
~~~~~~~
224
233
225
- .. fixture :: db
226
-
227
234
This fixture will ensure the Django database is set up. Only
228
235
required for fixtures that want to use the database themselves. A
229
- test function should normally use the `` pytest.mark.django_db ` `
236
+ test function should normally use the :func: ` pytest.mark.django_db `
230
237
mark to signal it needs the database. This fixture does
231
238
not return a database connection object. When you need a Django
232
239
database connection or cursor, import it from Django using
233
240
``from django.db import connection ``.
234
241
242
+ .. fixture :: transactional_db
243
+
235
244
``transactional_db ``
236
245
~~~~~~~~~~~~~~~~~~~~
237
246
238
247
This fixture can be used to request access to the database including
239
248
transaction support. This is only required for fixtures which need
240
249
database access themselves. A test function should normally use the
241
- ``pytest.mark.django_db `` mark with ``transaction=True ``.
250
+ func:`pytest.mark.django_db ` mark with ``transaction=True `` to signal
251
+ it needs the database.
252
+
253
+ .. fixture :: django_db_reset_sequences
242
254
243
255
``django_db_reset_sequences ``
244
256
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245
257
246
- .. fixture :: django_db_reset_sequences
247
-
248
258
This fixture provides the same transactional database access as
249
- ``transactional_db ``, with additional support for reset of auto increment
250
- sequences (if your database supports it). This is only required for
251
- fixtures which need database access themselves. A test function should
252
- normally use the ``pytest.mark.django_db `` mark with ``transaction=True `` and ``reset_sequences=True ``.
259
+ :fixture: `transactional_db `, with additional support for reset of auto
260
+ increment sequences (if your database supports it). This is only required for
261
+ fixtures which need database access themselves. A test function should normally
262
+ use the :func: `pytest.mark.django_db ` mark with ``transaction=True `` and
263
+ ``reset_sequences=True ``.
264
+
265
+ .. fixture :: live_server
253
266
254
267
``live_server ``
255
268
~~~~~~~~~~~~~~~
@@ -272,6 +285,8 @@ also directly concatenate a string to form a URL: ``live_server +
272
285
In addition, using ``live_server `` will also trigger transactional
273
286
database access, if not specified.
274
287
288
+ .. fixture :: settings
289
+
275
290
``settings ``
276
291
~~~~~~~~~~~~
277
292
@@ -341,6 +356,7 @@ Example usage::
341
356
Item.objects.create('foo')
342
357
Item.objects.create('bar')
343
358
359
+ .. fixture :: mailoutbox
344
360
345
361
``mailoutbox ``
346
362
~~~~~~~~~~~~~~
0 commit comments