Skip to content

[Docs] Manual TestClient creation should be async #5311

@ErwanDL

Description

@ErwanDL

🐞 Describe the bug
In the Framework agnostic utilities section of the "Testing" docs, this code snippet indicates how to manually create a TestClient :

with loop_context() as loop:
    app = _create_example_app()
    with TestClient(TestServer(app), loop=loop) as client:

        async def test_get_route():
            nonlocal client
            resp = await client.get("/")
            assert resp.status == 200
            text = await resp.text()
            assert "Hello, world" in text

        loop.run_until_complete(test_get_route())

However, when trying to create a TestClient that way, you get the following error :

TypeError: Use async with instead
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fc6daa51a60>

I suspect the docs should rather indicate that the TestClient be created that way :

async with TestClient(TestServer(app), loop=loop) as client:
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions