Skip to content

Commit 0e827fb

Browse files
committed
Add an async_api_client pytest fixture
1 parent 711ad5f commit 0e827fb

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

tests/conftest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@
55
"""
66
import pytest
77
from fastapi.testclient import TestClient
8+
from async_asgi_testclient import TestClient as AsyncTestClient
89

910
from app.main import APP
1011

1112

1213
@pytest.fixture
1314
def api_client():
1415
"""
15-
Returns a TestClient.
16+
Returns a fastapi.testclient.TestClient.
1617
The test client uses the requests library for making http requests.
1718
"""
1819
return TestClient(APP)
20+
21+
22+
@pytest.fixture
23+
async def async_api_client():
24+
"""
25+
Returns an async_asgi_testclient.TestClient.
26+
"""
27+
return AsyncTestClient(APP)

tests/test_routes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ async def test_v2_locations_id(self, mock_datetime):
151151
({"source": "jhu", "country_code": "US"}, 404),
152152
],
153153
)
154-
async def test_locations_status_code(query_params, expected_status):
155-
api_client = TestClient(APP)
154+
async def test_locations_status_code(async_api_client, query_params, expected_status):
156155
async with mock_client_session() as mocked_client_session:
157156
mocked_client_session.get = mocked_session_get
158-
response = await api_client.get("/v2/locations", query_string=query_params)
157+
response = await async_api_client.get("/v2/locations", query_string=query_params)
159158

160159
print(f"GET {response.url}\n{response}")
161160
print(f"\tjson:\n{pf(response.json())[:1000]}\n\t...")
@@ -174,11 +173,10 @@ async def test_locations_status_code(query_params, expected_status):
174173
{"source": "jhu", "timelines": True},
175174
],
176175
)
177-
async def test_latest(query_params):
178-
api_client = TestClient(APP)
176+
async def test_latest(async_api_client, query_params):
179177
async with mock_client_session() as mocked_client_session:
180178
mocked_client_session.get = mocked_session_get
181-
response = await api_client.get("/v2/latest", query_string=query_params)
179+
response = await async_api_client.get("/v2/latest", query_string=query_params)
182180

183181
print(f"GET {response.url}\n{response}")
184182

0 commit comments

Comments
 (0)