@@ -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
2323will run in its own transaction which will be rolled back at the end
2424of the test. This behavior is the same as Django's standard
2525`django.test.TestCase `_ class.
@@ -137,12 +137,23 @@ Example
137137 response = client.get('/')
138138 assert response.content == 'Foobar'
139139
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+
140152
141153``admin_client `` - ``django.test.Client `` logged in as admin
142154~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143155
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.
146157
147158Example
148159"""""""
@@ -153,36 +164,49 @@ Example
153164 response = admin_client.get('/admin/')
154165 assert response.status_code == 200
155166
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) .
158169
159170``admin_user `` - a admin user (superuser)
160171~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161172
162173An instance of a superuser, with username "admin" and password "password" (in
163174case there is no "admin" user yet).
164175
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) .
167178
168179
169180``django_user_model ``
170181~~~~~~~~~~~~~~~~~~~~~
171182
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+
173195
174196``django_username_field ``
175197~~~~~~~~~~~~~~~~~~~~~~~~~
176198
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.
178202
179203``db ``
180204~~~~~~~
181205
182206.. fixture :: db
183207
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
186210test function should normally use the :py:func: `~pytest.mark.django_db `
187211mark to signal it needs the database.
188212
@@ -242,7 +266,7 @@ Example
242266``mailoutbox ``
243267~~~~~~~~~~~~~~~~~~~~~~~~~
244268
245- A clean mail outbox where Django emails are being sent to .
269+ A clean email outbox to which Django-generated emails are sent.
246270
247271Example
248272"""""""
@@ -261,65 +285,23 @@ Example
261285 assert list(m.to) == ['to@example.com']
262286
263287
264- Environment autouse fixtures
265- ----------------------------
288+ Automatic cleanup
289+ -----------------
266290
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
269292during tests.
270293
271-
272294Clearing of site cache
273295~~~~~~~~~~~~~~~~~~~~~~
274296
275297If ``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
277299object to be returned by ``Site.objects.get_current() ``.
278300
279301
280302Clearing of mail.outbox
281303~~~~~~~~~~~~~~~~~~~~~~~
282304
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