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

[Docs] Manual TestClient creation should be async #5311

Closed
ErwanDL opened this issue Dec 6, 2020 · 1 comment · Fixed by #8874
Closed

[Docs] Manual TestClient creation should be async #5311

ErwanDL opened this issue Dec 6, 2020 · 1 comment · Fixed by #8874
Labels
bug documentation Improvements or additions to documentation

Comments

@ErwanDL
Copy link

ErwanDL commented Dec 6, 2020

🐞 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:
    ...
@ErwanDL ErwanDL added the bug label Dec 6, 2020
@asvetlov
Copy link
Member

asvetlov commented Dec 6, 2020

Correct.
I also want to get rid of explicit loops in examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants