Get startedGet started for free

FastAPI automated testing

1. FastAPI automated testing

Welcome back! Now that we have learned how to support all four kinds of operations with FastAPI with error handling, let's learn how to test our endpoints automatically.

2. What Are Automated Tests?

Let's begin by defining automated tests. The two kinds of automated tests we can build with FastAPI are unit tests and system tests. While unit tests focus on isolated code, system tests focus on an isolated system operations. This means that unit tests are designed to validate how specific units of code function, whereas system tests are designed to validate specific parts of the system function. In the case of FastAPI, unit tests are scoped to a function or method. System tests are scoped to an endpoint or other specific parts of the application. Technically there is one very important difference between the two kinds of tests. To run unit tests, you need only an isolated Python environment with necessary dependencies. To run system tests, you need a Python environment with access to the running application. Look at how we can test an example endpoint called main with unit tests versus system tests. On the left, the unit test can call the function main and validate the dictionary it returns. On the right, the system test can call the actual endpoint, validate the status of the response, and validate that the JSON response parses into the same dictionary.

3. Using TestClient

FastAPI lets us write automated system tests just like unit tests. To enable this, the TestClient class lets pytest call the running API and validate its responses. In our system tests, we can import TestClient from fastapi.testclient and import our app from main.py, the local file that defines our FastAPI server. Then we can create a client object with the application context by passing in the app. Each of our system tests can use the client to call endpoints in our running application and validate any aspect of the full HTTP response, as you can see in this example. Given an appropriate main.py, this is a fully executable pytest script with the system test from the last slide.

4. Testing Error or Failure Responses

How do we test error or failure responses from our app with system tests? Unlike unit tests, where we would have to catch an exception, FastAPI system tests let us test error or failure responses just like success responses, by validating the HTTP status code and response body. Here on the left we have a delete item endpoint that we defined in a prior video which sends status code 404 and an item not found message when the item does not exist. With system testing, we can test this endpoint just like a successful response, which we see on the right. This follows the same format of a test for a success response. It sends the request, validates the status code, and validates the parsed JSON output.

5. Let's practice!

Now, let's practice writing system tests in FastAPI!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.