Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testing utility examples #8874

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,12 @@ Framework Agnostic Utilities

High level test creation::

from aiohttp.test_utils import TestClient, TestServer, loop_context
from aiohttp.test_utils import TestClient, TestServer
from aiohttp import request

# loop_context is provided as a utility. You can use any
# asyncio.BaseEventLoop class in its place.
with loop_context() as loop:
async def test():
app = _create_example_app()
with TestClient(TestServer(app), loop=loop) as client:
async with TestClient(TestServer(app)) as client:

async def test_get_route():
nonlocal client
Expand All @@ -512,18 +510,18 @@ High level test creation::
text = await resp.text()
assert "Hello, world" in text

loop.run_until_complete(test_get_route())
await test_get_route()


If it's preferred to handle the creation / teardown on a more granular
basis, the TestClient object can be used directly::

from aiohttp.test_utils import TestClient, TestServer

with loop_context() as loop:
async def test():
app = _create_example_app()
client = TestClient(TestServer(app), loop=loop)
loop.run_until_complete(client.start_server())
client = TestClient(TestServer(app))
await client.start_server()
root = "http://127.0.0.1:{}".format(port)

async def test_get_route():
Expand All @@ -532,8 +530,8 @@ basis, the TestClient object can be used directly::
text = await resp.text()
assert "Hello, world" in text

loop.run_until_complete(test_get_route())
loop.run_until_complete(client.close())
await test_get_route()
await client.close()


A full list of the utilities provided can be found at the
Expand Down
Loading