Skip to content

Commit

Permalink
[PR #8874/d6a677d1 backport][3.11] Update testing utility examples (#…
Browse files Browse the repository at this point in the history
…8897)

**This is a backport of PR #8874 as merged into master
(d6a677d).**

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Aug 26, 2024
1 parent b2e1294 commit 275985c
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,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 @@ -465,18 +463,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 @@ -485,8 +483,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

0 comments on commit 275985c

Please sign in to comment.