TestClient waits for background tasks
See original GitHub issueTrying to write tests for my app based on Starlette, I noticed that queries I do against my API using TestClient
take a long time to complete because the client waits for background tasks spawned on the server.
This behavior slows done my tests unnecessarily, since I don’t care about the completion of those background tasks in most cases. Is there a way to disable it and make the TestClient
behave like a normal client of the API?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Background Tasks - FastAPI
You can define background tasks to be run after returning a response. ... have to be waiting for the operation to complete before...
Read more >tiangolo/fastapi - Gitter
... caplog): # test async background, waiting 3s, getting logs async with ... no wait, no log either :) async with TestClient(fastapi_testapp) as...
Read more >Test Client - Starlette
The test client allows you to make requests against your ASGI application, ... This ensure that the background thread on which the ASGI...
Read more >How to start a Uvicorn + FastAPI in background when testing ...
I want to start the server in a fixture when I start the tests, so when the test complete, the fixture will kill...
Read more >Testing asynchronous background workers in .NET
But what should it wait for? Under our background worker model, there's some continuation code that runs on the GUI thread by means...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Would there be a relatively easy way to
TestClient
request not wait for the background task to finish (i.e. return as soon as the HTTP response is complete) andOne of my endpoints starts an operation in the background that takes a few seconds to finish. During this time, additional calls to the same endpoint should result in an error. In other words: The endpoint is locked while the operation is running. I want to test that locking behavior by firing a second request while the background task is still running, but I can’t, since the first
TestClient
request will wait for the task to finish before returning.I would be fine with
TestClient
instead waiting for background tasks before the whole test returns (e.g. onTestClient
destruction), since my background task doesn’t run that long and it wouldn’t slow my test suite down too much.or you can mock your background tasks @ra-kete and check they’ve been called if hey’re async you can patch using CoroutineMock from asynctest package or wait 3.8