Skip to content

Commit 3e8ff2a

Browse files
authored
add async_client and async_rf fixtures (#865)
1 parent ded4186 commit 3e8ff2a

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

docs/helpers.rst

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ Example
135135
response = my_view(request)
136136
assert response.status_code == 200
137137

138+
.. fixture:: async_rf
139+
140+
``async_rf`` - ``AsyncRequestFactory``
141+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142+
143+
An instance of a `django.test.AsyncRequestFactory`
144+
145+
.. _django.test.AsyncRequestFactory: https://docs.djangoproject.com/en/3.1/topics/testing/advanced/#asyncrequestfactory
146+
147+
Example
148+
"""""""
149+
150+
::
151+
152+
from myapp.views import my_view
153+
154+
@pytest.mark.asyncio
155+
async def test_details(async_rf):
156+
request = await async_rf.get('/customer/details')
157+
response = my_view(request)
158+
assert response.status_code == 200
159+
138160
.. fixture:: client
139161

140162
``client`` - ``django.test.Client``
@@ -168,6 +190,25 @@ To use `client` as an authenticated standard user, call its
168190
response = client.get('/private')
169191
assert response.content == 'Protected Area'
170192

193+
.. fixture:: async_client
194+
195+
``async_client`` - ``django.test.AsyncClient``
196+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197+
198+
An instance of a `django.test.AsyncClient`
199+
200+
.. _django.test.AsyncClient: https://docs.djangoproject.com/en/stable/topics/testing/tools/#the-test-client
201+
202+
Example
203+
"""""""
204+
205+
::
206+
207+
@pytest.mark.asyncio
208+
async def test_with_async_client(async_client):
209+
response = await async_client.get('/')
210+
assert response.content == 'Foobar'
211+
171212
.. fixture:: admin_client
172213

173214
``admin_client`` - ``django.test.Client`` logged in as admin
@@ -235,7 +276,7 @@ This fixture will ensure the Django database is set up. Only
235276
required for fixtures that want to use the database themselves. A
236277
test function should normally use the :func:`pytest.mark.django_db`
237278
mark to signal it needs the database. This fixture does
238-
not return a database connection object. When you need a Django
279+
not return a database connection object. When you need a Django
239280
database connection or cursor, import it from Django using
240281
``from django.db import connection``.
241282

pytest_django/fixtures.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"django_user_model",
2121
"django_username_field",
2222
"client",
23+
"async_client",
2324
"admin_client",
2425
"rf",
26+
"async_rf",
2527
"settings",
2628
"live_server",
2729
"_live_server_helper",
@@ -261,6 +263,16 @@ def client():
261263
return Client()
262264

263265

266+
@pytest.fixture()
267+
def async_client():
268+
"""A Django test async client instance."""
269+
skip_if_no_django()
270+
271+
from django.test.client import AsyncClient
272+
273+
return AsyncClient()
274+
275+
264276
@pytest.fixture()
265277
def django_user_model(db):
266278
"""The class of Django's user model."""
@@ -322,6 +334,16 @@ def rf():
322334
return RequestFactory()
323335

324336

337+
@pytest.fixture()
338+
def async_rf():
339+
"""AsyncRequestFactory instance"""
340+
skip_if_no_django()
341+
342+
from django.test.client import AsyncRequestFactory
343+
344+
return AsyncRequestFactory()
345+
346+
325347
class SettingsWrapper:
326348
_to_restore = []
327349

pytest_django/plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
from .fixtures import _live_server_helper # noqa
2828
from .fixtures import admin_client # noqa
2929
from .fixtures import admin_user # noqa
30+
from .fixtures import async_client # noqa
3031
from .fixtures import client # noqa
3132
from .fixtures import db # noqa
3233
from .fixtures import django_user_model # noqa
3334
from .fixtures import django_username_field # noqa
3435
from .fixtures import live_server # noqa
3536
from .fixtures import django_db_reset_sequences # noqa
37+
from .fixtures import async_rf # noqa
3638
from .fixtures import rf # noqa
3739
from .fixtures import settings # noqa
3840
from .fixtures import transactional_db # noqa

tests/test_fixtures.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from django.utils.encoding import force_str
1919

2020
from pytest_django_test.app.models import Item
21+
from pytest_django.lazy_django import get_django_version
2122

2223

2324
@contextmanager
@@ -36,6 +37,13 @@ def test_client(client):
3637
assert isinstance(client, Client)
3738

3839

40+
@pytest.mark.skipif(get_django_version() < (3, 1), reason="Django >= 3.1 required")
41+
def test_async_client(async_client):
42+
from django.test.client import AsyncClient
43+
44+
assert isinstance(async_client, AsyncClient)
45+
46+
3947
@pytest.mark.django_db
4048
def test_admin_client(admin_client):
4149
assert isinstance(admin_client, Client)
@@ -73,6 +81,13 @@ def test_rf(rf):
7381
assert isinstance(rf, RequestFactory)
7482

7583

84+
@pytest.mark.skipif(get_django_version() < (3, 1), reason="Django >= 3.1 required")
85+
def test_async_rf(async_rf):
86+
from django.test.client import AsyncRequestFactory
87+
88+
assert isinstance(async_rf, AsyncRequestFactory)
89+
90+
7691
@pytest.mark.django_db
7792
def test_django_assert_num_queries_db(request, django_assert_num_queries):
7893
with nonverbose_config(request.config):

0 commit comments

Comments
 (0)