FastAPI's Test Client
This is my first blog post!
Please enjoy, and let me know if you have any feedback.
Abstract
If you are new to FastAPI, you might benefit from reading the following:
If you already know stuff about FastAPI, you might jump to:
Today, we'll talk about the main tool for testing FastAPI applications: the TestClient.
TestClient origin and features
The TestClient is a feature from Starlette
(one of the two main dependencies of FastAPI). On which, FastAPI only does a reimport on the testclient module,
as we can see here.
We can use the TestClient to test our WebSocket
and HTTP endpoints.
The TestClient weird behavior
Although documented on both FastAPI and
Starlette's documentation, most
of the people are not aware of the TestClient's behavior when it comes to events. To put it simple,
there are two ways of creating a TestClient object, and in one of those ways, the events are not
executed.
Let's see the behavior with the following FastAPI application:
| main.py | |
|---|---|
-
There are only two events available: startup and shutdown.
Read more about it on the ASGI documentation.
As you can see, there's a single endpoint, which gives us a different message depending on the value of the started variable.
The started variable is set to True on the startup event.
Now, let's test it with the TestClient:
As you can see, the above test passes. Which means the startup event was not triggered.
On the other hand, if we run the following test, we'll get a different result:
When used as context manager,
the TestClient will trigger the startup event.
The Future of the TestClient
By the moment I'm writing this blog, the latest FastAPI version is 0.83.0 with Starlette pinned on 0.19.1.
Starlette is already on version 0.20.3, and the next release will change the internals of the TestClient. To be
more specific, the HTTP client will be changed from requests to httpx.
As there are some differences between the two clients, the TestClient will reflect the same differences.
This change will be in Starlette on version 0.21.0, and I'm unsure when it will land on FastAPI.
Let's see the changes you should be aware:
allow_redirectswill be now calledfollow_redirects.cookiesparameter will be deprecated under method calls (it should be used on the client instantiation).dataparameter will be calledcontentwhen sending bytes or text.content_typewill default to "text/plain" when sending file instead of empty string.- The HTTP methods DELETE, GET, HEAD and OPTIONS will not accept
content,data,jsonandfilesparameters. -
dataparameter doesn't accept list of tuples, instead it should be a dictionary.
Those changes will likely impact your test suite. Having this in mind, I've created a codemod that will help you to migrate your tests: bump-testclient.
Here is the list of what the codemod will do:
- Replace
allow_redirectswithfollow_redirects. - Replace
datawithcontentwhen sending bytes or text. - Replace
client.<method>(..., <parameter>=...)byclient.request("<method>", ..., <parameter>=...)when parameter is eithercontent,data,jsonorfiles.
In case you want to read more about the differences between the underneath clients, you can check the httpx documentation.
Thanks for reading till here!