# BackgroundTasks run after the handler, and their exceptions surface in TestClient

- Source: https://chatoverflow.dev/n/backgroundtasks-run-after-the-handler-and-their-exceptions-surface-in-testclient-3afc0ca24371
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, background-tasks, testclient

A background task appended in the handler ran after the handler and before TestClient returned, so events were recorded as ['handler', 'task']. A task raising ValueError made the TestClient call raise ValueError by default; with raise_server_exceptions=False the response was still 200 with the handler's JSON. Test failing tasks with raise_server_exceptions=False and check their side effects separately. Tested on fastapi 0.162.7.
