@@ -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.
@@ -163,12 +163,23 @@ Example
163
163
response = client.get('/')
164
164
assert response.content == 'Foobar'
165
165
166
+ To use `client ` as an authenticated standard user, call its `login() ` method before accessing a URL:
167
+
168
+ ::
169
+
170
+ def test_with_authenticated_client(client, django_user_model):
171
+ username = "user1"
172
+ password = "bar"
173
+ django_user_model.objects.create(username=username, password=password)
174
+ client.login(username=username, password=password)
175
+ response = client.get('/private')
176
+ assert response.content == 'Protected Area'
177
+
166
178
167
179
``admin_client `` - ``django.test.Client `` logged in as admin
168
180
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169
181
170
- An instance of a `django.test.Client `_,
171
- that is logged in as an admin user.
182
+ An instance of a `django.test.Client `_, logged in as an admin user.
172
183
173
184
Example
174
185
"""""""
@@ -179,36 +190,49 @@ Example
179
190
response = admin_client.get('/admin/')
180
191
assert response.status_code == 200
181
192
182
- As an extra bonus this will automatically mark the database using the
183
- ``django_db `` mark.
193
+ Using the ` admin_client ` fixture will cause the test to automatically be marked for database use (no need to specify the
194
+ ``django_db `` mark) .
184
195
185
196
``admin_user `` - a admin user (superuser)
186
197
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
198
188
199
An instance of a superuser, with username "admin" and password "password" (in
189
200
case there is no "admin" user yet).
190
201
191
- As an extra bonus this will automatically mark the database using the
192
- ``django_db `` mark.
202
+ Using the ` admin_user ` fixture will cause the test to automatically be marked for database use (no need to specify the
203
+ ``django_db `` mark) .
193
204
194
205
195
206
``django_user_model ``
196
207
~~~~~~~~~~~~~~~~~~~~~
197
208
198
- The user model used by Django. This handles different versions of Django.
209
+ A shortcut to the User model configured for use by the current Django project (aka the model referenced by
210
+ `settings.AUTH_USER_MODEL `). Use this fixture to make pluggable apps testable regardless what User model is configured
211
+ in the containing Django project.
212
+
213
+ Example
214
+ """""""
215
+
216
+ ::
217
+
218
+ def test_new_user(django_user_model):
219
+ django_user_model.objects.create(username="someone", password="something")
220
+
199
221
200
222
``django_username_field ``
201
223
~~~~~~~~~~~~~~~~~~~~~~~~~
202
224
203
- The field name used for the username on the user model.
225
+ This fixture extracts the field name used for the username on the user model, i.e. resolves to the current
226
+ ``settings.USERNAME_FIELD ``. Use this fixture to make pluggable apps testable regardless what the username field
227
+ is configured to be in the containing Django project.
204
228
205
229
``db ``
206
230
~~~~~~~
207
231
208
232
.. fixture :: db
209
233
210
- This fixture will ensure the Django database is set up. This only
211
- required for fixtures which want to use the database themselves. A
234
+ This fixture will ensure the Django database is set up. Only
235
+ required for fixtures that want to use the database themselves. A
212
236
test function should normally use the :py:func: `~pytest.mark.django_db `
213
237
mark to signal it needs the database.
214
238
@@ -268,7 +292,7 @@ Example
268
292
``mailoutbox ``
269
293
~~~~~~~~~~~~~~~~~~~~~~~~~
270
294
271
- A clean mail outbox where Django emails are being sent to .
295
+ A clean email outbox to which Django-generated emails are sent.
272
296
273
297
Example
274
298
"""""""
@@ -287,65 +311,23 @@ Example
287
311
assert list(m.to) == ['to@example.com']
288
312
289
313
290
- Environment autouse fixtures
291
- ----------------------------
314
+ Automatic cleanup
315
+ -----------------
292
316
293
- pytest-django provides some pytest fixtures that are of autouse
294
- nature. They provide functionality to assure a clean environment
317
+ pytest-django provides some functionality to assure a clean and consistent environment
295
318
during tests.
296
319
297
-
298
320
Clearing of site cache
299
321
~~~~~~~~~~~~~~~~~~~~~~
300
322
301
323
If ``django.contrib.sites `` is in your INSTALLED_APPS, Site cache will
302
- be cleared for each test to avoid hitting the cache and cause wrong Site
324
+ be cleared for each test to avoid hitting the cache and causing the wrong Site
303
325
object to be returned by ``Site.objects.get_current() ``.
304
326
305
327
306
328
Clearing of mail.outbox
307
329
~~~~~~~~~~~~~~~~~~~~~~~
308
330
309
- ``mail.outbox `` will be cleared for each pytest, to give tests a empty
310
- mailbox. It is however more pytestic to use the ``mailoutbox `` fixture
311
- to access ``mail.outbox ``.
312
-
313
-
314
- Examples
315
- --------
316
-
317
-
318
- Example with ``rf ``, ``admin_user ``, fixture and class-based views
319
- """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
320
-
321
- ::
322
-
323
- import pytest
324
-
325
- from django.core.urlresolvers import reverse
326
- from myapp.models import Thing
327
- from myapp.views import ThingDetailView
328
-
329
- @pytest.fixture
330
- def thing(admin_user):
331
- (thing_object, created) = Thing.objects.get_or_create(name="test",
332
- created_by=admin_user)
333
- return thing_object
334
-
335
- def test_detail_view_logged_in(rf, admin_user, thing):
336
- # set kwargs for reverse and for view
337
- kwargs_thing = {
338
- 'pk': thing.id,
339
- }
340
-
341
- url = reverse("thing_detail", kwargs=kwargs_thing)
342
-
343
- # bind url to request factory
344
- request = rf.get(url)
345
-
346
- # set user in request to admin_user
347
- request.user = admin_user
348
-
349
- # creates response, given request and kwargs needed for view
350
- response = ThingDetailView.as_view()(request, **kwargs_thing)
351
- assert response.status_code == 200
331
+ ``mail.outbox `` will be cleared for each pytest, to give each new test an empty
332
+ mailbox to work with. However, it's more "pytestic" to use the ``mailoutbox `` fixture described above
333
+ than to access ``mail.outbox ``.
0 commit comments