@@ -19,7 +19,7 @@ on what marks are and for notes on using_ them.
19
19
.. :py:function:: pytest.mark.django_db:
20
20
21
21
This is used to mark a test function as requiring the database. It
22
- will ensure the database is setup correctly for the test. Each test
22
+ will ensure the database is set up correctly for the test. Each test
23
23
will run in its own transaction which will be rolled back at the end
24
24
of the test. This behavior is the same as Django's standard
25
25
`django.test.TestCase `_ class.
@@ -137,12 +137,23 @@ Example
137
137
response = client.get('/')
138
138
assert response.content == 'Foobar'
139
139
140
+ To use `client ` as an authenticated standard user, call its `login() ` method before accessing a URL:
141
+
142
+ ::
143
+
144
+ def test_with_authenticated_client(client, django_user_model):
145
+ username = "user1"
146
+ password = "bar"
147
+ django_user_model.objects.create(username=username, password=password)
148
+ client.login(username=username, password=password)
149
+ response = client.get('/private')
150
+ assert response.content == 'Protected Area'
151
+
140
152
141
153
``admin_client `` - ``django.test.Client `` logged in as admin
142
154
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143
155
144
- An instance of a `django.test.Client `_,
145
- that is logged in as an admin user.
156
+ An instance of a `django.test.Client `_, logged in as an admin user.
146
157
147
158
Example
148
159
"""""""
@@ -153,36 +164,49 @@ Example
153
164
response = admin_client.get('/admin/')
154
165
assert response.status_code == 200
155
166
156
- As an extra bonus this will automatically mark the database using the
157
- ``django_db `` mark.
167
+ Using the ` admin_client ` fixture will cause the test to automatically be marked for database use (no need to specify the
168
+ ``django_db `` mark) .
158
169
159
170
``admin_user `` - a admin user (superuser)
160
171
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
172
162
173
An instance of a superuser, with username "admin" and password "password" (in
163
174
case there is no "admin" user yet).
164
175
165
- As an extra bonus this will automatically mark the database using the
166
- ``django_db `` mark.
176
+ Using the ` admin_user ` fixture will cause the test to automatically be marked for database use (no need to specify the
177
+ ``django_db `` mark) .
167
178
168
179
169
180
``django_user_model ``
170
181
~~~~~~~~~~~~~~~~~~~~~
171
182
172
- The user model used by Django. This handles different versions of Django.
183
+ A shortcut to the User model configured for use by the current Django project (aka the model referenced by
184
+ `settings.AUTH_USER_MODEL `). Use this fixture to make pluggable apps testable regardless what User model is configured
185
+ in the containing Django project.
186
+
187
+ Example
188
+ """""""
189
+
190
+ ::
191
+
192
+ def test_new_user(django_user_model):
193
+ django_user_model.objects.create(username="someone", password="something")
194
+
173
195
174
196
``django_username_field ``
175
197
~~~~~~~~~~~~~~~~~~~~~~~~~
176
198
177
- The field name used for the username on the user model.
199
+ This fixture extracts the field name used for the username on the user model, i.e. resolves to the current
200
+ ``settings.USERNAME_FIELD ``. Use this fixture to make pluggable apps testable regardless what the username field
201
+ is configured to be in the containing Django project.
178
202
179
203
``db ``
180
204
~~~~~~~
181
205
182
206
.. fixture :: db
183
207
184
- This fixture will ensure the Django database is set up. This only
185
- required for fixtures which want to use the database themselves. A
208
+ This fixture will ensure the Django database is set up. Only
209
+ required for fixtures that want to use the database themselves. A
186
210
test function should normally use the :py:func: `~pytest.mark.django_db `
187
211
mark to signal it needs the database.
188
212
@@ -242,7 +266,7 @@ Example
242
266
``mailoutbox ``
243
267
~~~~~~~~~~~~~~~~~~~~~~~~~
244
268
245
- A clean mail outbox where Django emails are being sent to .
269
+ A clean email outbox to which Django-generated emails are sent.
246
270
247
271
Example
248
272
"""""""
@@ -261,65 +285,23 @@ Example
261
285
assert list(m.to) == ['[email protected] ']
262
286
263
287
264
- Environment autouse fixtures
265
- ----------------------------
288
+ Automatic cleanup
289
+ -----------------
266
290
267
- pytest-django provides some pytest fixtures that are of autouse
268
- nature. They provide functionality to assure a clean environment
291
+ pytest-django provides some functionality to assure a clean and consistent environment
269
292
during tests.
270
293
271
-
272
294
Clearing of site cache
273
295
~~~~~~~~~~~~~~~~~~~~~~
274
296
275
297
If ``django.contrib.sites `` is in your INSTALLED_APPS, Site cache will
276
- be cleared for each test to avoid hitting the cache and cause wrong Site
298
+ be cleared for each test to avoid hitting the cache and causing the wrong Site
277
299
object to be returned by ``Site.objects.get_current() ``.
278
300
279
301
280
302
Clearing of mail.outbox
281
303
~~~~~~~~~~~~~~~~~~~~~~~
282
304
283
- ``mail.outbox `` will be cleared for each pytest, to give tests a empty
284
- mailbox. It is however more pytestic to use the ``mailoutbox `` fixture
285
- to access ``mail.outbox ``.
286
-
287
-
288
- Examples
289
- --------
290
-
291
-
292
- Example with ``rf ``, ``admin_user ``, fixture and class-based views
293
- """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
294
-
295
- ::
296
-
297
- import pytest
298
-
299
- from django.core.urlresolvers import reverse
300
- from myapp.models import Thing
301
- from myapp.views import ThingDetailView
302
-
303
- @pytest.fixture
304
- def thing(admin_user):
305
- (thing_object, created) = Thing.objects.get_or_create(name="test",
306
- created_by=admin_user)
307
- return thing_object
308
-
309
- def test_detail_view_logged_in(rf, admin_user, thing):
310
- # set kwargs for reverse and for view
311
- kwargs_thing = {
312
- 'pk': thing.id,
313
- }
314
-
315
- url = reverse("thing_detail", kwargs=kwargs_thing)
316
-
317
- # bind url to request factory
318
- request = rf.get(url)
319
-
320
- # set user in request to admin_user
321
- request.user = admin_user
322
-
323
- # creates response, given request and kwargs needed for view
324
- response = ThingDetailView.as_view()(request, **kwargs_thing)
325
- assert response.status_code == 200
305
+ ``mail.outbox `` will be cleared for each pytest, to give each new test an empty
306
+ mailbox to work with. However, it's more "pytestic" to use the ``mailoutbox `` fixture described above
307
+ than to access ``mail.outbox ``.
0 commit comments