-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

FastAPI Cookbook
By :

Once we setup our testing environment, we can focus on the process of writing and executing tests for FastAPI applications. Unit tests are essential for validating the behaviour of individual parts of your application in isolation, ensuring they perform as expected. In this recipe, you will learn to test the endpoints of your application.
We will use pytest
to test the FastAPI client in unit tests. Since the recipe will utilize common testing fixtures, used in most Python standard code, make sure to be familiar with the test fixtures before diving into the recipe. If this is not the case, you can always refer to the dedicated documentation page at the link: https://docs.pytest.org/en/7.1.x/reference/fixtures.html.
We will start by creating a unit test for the same GET /home
endpoint, but differently from the previous recipe. We will use the TestClient
class provided by FastAPI.
Let’s create a fixture...