question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestClient waits for background tasks

See original GitHub issue

Trying 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:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
scycommented, Aug 20, 2021

Would there be a relatively easy way to

  • still run the background task (i.e. not mock it away)
  • make the TestClient request not wait for the background task to finish (i.e. return as soon as the HTTP response is complete) and
  • not cancel the background task?

One 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. on TestClient destruction), since my background task doesn’t run that long and it wouldn’t slow my test suite down too much.

1reaction
euri10commented, Sep 13, 2019

Trying 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?

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found