Skip to content

Commit 9773b2d

Browse files
shackertimb07
authored andcommitted
Document use of authenticated client and django_user_model (pytest-dev#554)
- Includes misc documentation cleanup
1 parent d45b9dd commit 9773b2d

File tree

1 file changed

+43
-61
lines changed

1 file changed

+43
-61
lines changed

docs/helpers.rst

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ on what marks are and for notes on using_ them.
1919
.. :py:function:: pytest.mark.django_db:
2020
2121
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
2323
will run in its own transaction which will be rolled back at the end
2424
of the test. This behavior is the same as Django's standard
2525
`django.test.TestCase`_ class.
@@ -163,12 +163,23 @@ Example
163163
response = client.get('/')
164164
assert response.content == 'Foobar'
165165

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+
166178

167179
``admin_client`` - ``django.test.Client`` logged in as admin
168180
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169181

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.
172183

173184
Example
174185
"""""""
@@ -179,36 +190,49 @@ Example
179190
response = admin_client.get('/admin/')
180191
assert response.status_code == 200
181192

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).
184195

185196
``admin_user`` - a admin user (superuser)
186197
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187198

188199
An instance of a superuser, with username "admin" and password "password" (in
189200
case there is no "admin" user yet).
190201

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).
193204

194205

195206
``django_user_model``
196207
~~~~~~~~~~~~~~~~~~~~~
197208

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+
199221

200222
``django_username_field``
201223
~~~~~~~~~~~~~~~~~~~~~~~~~
202224

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.
204228

205229
``db``
206230
~~~~~~~
207231

208232
.. fixture:: db
209233

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
212236
test function should normally use the :py:func:`~pytest.mark.django_db`
213237
mark to signal it needs the database.
214238

@@ -268,7 +292,7 @@ Example
268292
``mailoutbox``
269293
~~~~~~~~~~~~~~~~~~~~~~~~~
270294

271-
A clean mail outbox where Django emails are being sent to.
295+
A clean email outbox to which Django-generated emails are sent.
272296

273297
Example
274298
"""""""
@@ -287,65 +311,23 @@ Example
287311
assert list(m.to) == ['to@example.com']
288312

289313

290-
Environment autouse fixtures
291-
----------------------------
314+
Automatic cleanup
315+
-----------------
292316

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
295318
during tests.
296319

297-
298320
Clearing of site cache
299321
~~~~~~~~~~~~~~~~~~~~~~
300322

301323
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
303325
object to be returned by ``Site.objects.get_current()``.
304326

305327

306328
Clearing of mail.outbox
307329
~~~~~~~~~~~~~~~~~~~~~~~
308330

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

Comments
 (0)