@@ -16,46 +16,43 @@ All of Django's :py:class:`~django:django.test.TestCase`
16
16
Markers
17
17
-------
18
18
19
- ``pytest-django `` registers and uses markers. See the pytest documentation _
20
- on what marks are and for notes on using _ them.
21
-
22
- .. _documentation : https://pytest.org/en/latest/mark.html
23
- .. _using : https://pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules
19
+ ``pytest-django `` registers and uses markers. See the pytest
20
+ :ref: `documentation <pytest:mark >` on what marks are and for notes on
21
+ :ref: `using <pytest:scoped-marking >` them.
24
22
25
23
26
24
``pytest.mark.django_db `` - request database access
27
25
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28
26
29
- .. :py:function:: pytest.mark.django_db([transaction=False, reset_sequences=False]):
30
-
31
- This is used to mark a test function as requiring the database. It
32
- will ensure the database is set up correctly for the test. Each test
33
- will run in its own transaction which will be rolled back at the end
34
- of the test. This behavior is the same as Django's standard
35
- `django.test.TestCase `_ class.
36
-
37
- In order for a test to have access to the database it must either
38
- be marked using the ``django_db `` mark or request one of the ``db ``,
39
- ``transactional_db `` or ``django_db_reset_sequences `` fixtures. Otherwise the
40
- test will fail when trying to access the database.
41
-
42
- :type transaction: bool
43
- :param transaction:
44
- The ``transaction `` argument will allow the test to use real transactions.
45
- With ``transaction=False `` (the default when not specified), transaction
46
- operations are noops during the test. This is the same behavior that
47
- `django.test.TestCase `_
48
- uses. When ``transaction=True ``, the behavior will be the same as
49
- `django.test.TransactionTestCase `_
50
-
51
-
52
- :type reset_sequences: bool
53
- :param reset_sequences:
54
- The ``reset_sequences `` argument will ask to reset auto increment sequence
55
- values (e.g. primary keys) before running the test. Defaults to
56
- ``False ``. Must be used together with ``transaction=True `` to have an
57
- effect. Please be aware that not all databases support this feature.
58
- For details see :py:attr: `django.test.TransactionTestCase.reset_sequences `.
27
+ .. py :function :: pytest.mark.django_db([transaction=False , reset_sequences=False ])
28
+
29
+ This is used to mark a test function as requiring the database. It
30
+ will ensure the database is set up correctly for the test. Each test
31
+ will run in its own transaction which will be rolled back at the end
32
+ of the test. This behavior is the same as Django's standard
33
+ :class: `~django.test.TestCase ` class.
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.
39
+
40
+ :type transaction: bool
41
+ :param transaction:
42
+ The ``transaction `` argument will allow the test to use real transactions.
43
+ With ``transaction=False `` (the default when not specified), transaction
44
+ operations are noops during the test. This is the same behavior that
45
+ :class: `django.test.TestCase ` uses. When ``transaction=True ``, the behavior
46
+ will be the same as :class: `django.test.TransactionTestCase `.
47
+
48
+
49
+ :type reset_sequences: bool
50
+ :param reset_sequences:
51
+ The ``reset_sequences `` argument will ask to reset auto increment sequence
52
+ values (e.g. primary keys) before running the test. Defaults to
53
+ ``False ``. Must be used together with ``transaction=True `` to have an
54
+ effect. Please be aware that not all databases support this feature.
55
+ For details see :py:attr: `django.test.TransactionTestCase.reset_sequences `.
59
56
60
57
.. note ::
61
58
@@ -68,13 +65,10 @@ test will fail when trying to access the database.
68
65
69
66
.. note :: Automatic usage with ``django.test.TestCase``.
70
67
71
- Test classes that subclass `django.test.TestCase `_ will have access to
68
+ Test classes that subclass :class: `django.test.TestCase ` will have access to
72
69
the database always to make them compatible with existing Django tests.
73
- Test classes that subclass Python's ``unittest.TestCase `` need to have the
74
- marker applied in order to access the database.
75
-
76
- .. _django.test.TestCase : https://docs.djangoproject.com/en/dev/topics/testing/overview/#testcase
77
- .. _django.test.TransactionTestCase : https://docs.djangoproject.com/en/dev/topics/testing/overview/#transactiontestcase
70
+ Test classes that subclass Python's :class: `unittest.TestCase ` need to have
71
+ the marker applied in order to access the database.
78
72
79
73
80
74
``pytest.mark.urls `` - override the urlconf
@@ -119,16 +113,14 @@ Fixtures
119
113
--------
120
114
121
115
pytest-django provides some pytest fixtures to provide dependencies for tests.
122
- More information on fixtures is available in the `pytest documentation
123
- <https:// pytest.org/en/latest/fixture.html> `_ .
116
+ More information on fixtures is available in the :ref: `pytest documentation
117
+ <pytest:fixtures>` .
124
118
125
119
126
120
``rf `` - ``RequestFactory ``
127
121
~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
122
129
- An instance of a `django.test.RequestFactory `_
130
-
131
- .. _django.test.RequestFactory : https://docs.djangoproject.com/en/dev/topics/testing/advanced/#django.test.RequestFactory
123
+ An instance of a :class: `django.test.RequestFactory `.
132
124
133
125
Example
134
126
"""""""
@@ -145,9 +137,7 @@ Example
145
137
``client `` - ``django.test.Client ``
146
138
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147
139
148
- An instance of a `django.test.Client `_
149
-
150
- .. _django.test.Client : https://docs.djangoproject.com/en/dev/topics/testing/tools/#the-test-client
140
+ An instance of a :class: `django.test.Client `.
151
141
152
142
Example
153
143
"""""""
@@ -158,8 +148,9 @@ Example
158
148
response = client.get('/')
159
149
assert response.content == 'Foobar'
160
150
161
- To use `client ` as an authenticated standard user, call its `force_login() ` or
162
- `login() ` method before accessing a URL:
151
+ To use `client ` as an authenticated standard user, call its
152
+ :meth: `force_login() <django.test.Client.force_login> ` or
153
+ :meth: `login() <django.test.Client.login()> ` method before accessing a URL:
163
154
164
155
::
165
156
@@ -178,7 +169,7 @@ To use `client` as an authenticated standard user, call its `force_login()` or
178
169
``admin_client `` - ``django.test.Client `` logged in as admin
179
170
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180
171
181
- An instance of a `django.test.Client `_ , logged in as an admin user.
172
+ An instance of a :class: `django.test.Client `, logged in as an admin user.
182
173
183
174
Example
184
175
"""""""
@@ -208,7 +199,8 @@ Using the `admin_user` fixture will cause the test to automatically be marked fo
208
199
~~~~~~~~~~~~~~~~~~~~~
209
200
210
201
A shortcut to the User model configured for use by the current Django project (aka the model referenced by
211
- `settings.AUTH_USER_MODEL `). Use this fixture to make pluggable apps testable regardless what User model is configured
202
+ `settings.AUTH_USER_MODEL <https://docs.djangoproject.com/en/stable/ref/settings/#auth-user-model >`_).
203
+ Use this fixture to make pluggable apps testable regardless what User model is configured
212
204
in the containing Django project.
213
205
214
206
Example
@@ -223,8 +215,9 @@ Example
223
215
``django_username_field ``
224
216
~~~~~~~~~~~~~~~~~~~~~~~~~
225
217
226
- This fixture extracts the field name used for the username on the user model, i.e. resolves to the current
227
- ``settings.USERNAME_FIELD ``. Use this fixture to make pluggable apps testable regardless what the username field
218
+ This fixture extracts the field name used for the username on the user model, i.e.
219
+ resolves to the user model's :attr: `~django.contrib.auth.models.CustomUser.USERNAME_FIELD `.
220
+ Use this fixture to make pluggable apps testable regardless what the username field
228
221
is configured to be in the containing Django project.
229
222
230
223
``db ``
@@ -264,7 +257,7 @@ normally use the ``pytest.mark.django_db`` mark with ``transaction=True`` and ``
264
257
265
258
This fixture runs a live Django server in a background thread. The
266
259
server's URL can be retrieved using the ``live_server.url `` attribute
267
- or by requesting it's string value: ``unicode (live_server) ``. You can
260
+ or by requesting it's string value: ``str (live_server) ``. You can
268
261
also directly concatenate a string to form a URL: ``live_server +
269
262
'/foo ``.
270
263
@@ -313,8 +306,8 @@ This fixture allows to check for an expected number of DB queries.
313
306
If the assertion failed, the executed queries can be shown by using
314
307
the verbose command line option.
315
308
316
- It wraps `django.test.utils.CaptureQueriesContext ` and yields the wrapped
317
- CaptureQueriesContext instance.
309
+ It wraps `` django.test.utils.CaptureQueriesContext ` ` and yields the wrapped
310
+ `` CaptureQueriesContext `` instance.
318
311
319
312
Example usage::
320
313
0 commit comments