Skip to main content
In this tutorial, we dive into testing a FastAPI application using the built-in FastAPI TestClient. We cover two primary examples: testing a standalone function (e.g., a “withdraw” function) and verifying FastAPI routes.

Example 1: Testing a Sample “withdraw” Function

Consider a simple function that checks whether an account has sufficient funds before permitting a withdrawal. If the requested amount exceeds the available balance, an exception is raised to simulate an error condition:
When testing this function, an error might be raised unexpectedly. For example, you might encounter output like this:
This output indicates that a test failed due to a ZeroDivisionError instead of the expected behavior.
When writing tests for functions that can raise exceptions, ensure your test framework is set up to capture and assert on these exceptions appropriately.

Example 2: Testing FastAPI Routes Using the TestClient

FastAPI provides a built-in TestClient, which simulates HTTP requests to your application similarly to the popular Requests library. This allows you to test your routes efficiently without running a live server.

Setting Up a Basic Route for Testing

Create a simple FastAPI application with a route that returns a greeting message:
In this example, the TestClient is initialized by passing the FastAPI instance (app) to it. The test sends a GET request to the root route and verifies that the response status code is 200 and the JSON payload matches the expected message.

How the TestClient Works

The FastAPI TestClient operates much like a Requests session object. This means you can customize your HTTP requests by setting methods (GET, POST, etc.), headers, payloads, or authorization credentials. For comparison, here’s a Requests example:
While this sample uses Requests, FastAPI’s TestClient allows you to perform similar operations directly within your test suite. For user functionalities such as creating users and retrieving profiles, you can create separate test modules (for example, tests/test_users.py). To test these routes, import the FastAPI application (commonly defined in app/main.py) and instantiate the TestClient:
Printing the response initially helps you inspect the returned payload. Later, you can include assertions to validate the content more precisely.

Verifying Response Content and Status Code

After confirming the output, enhance your tests with assertions to check both the response payload and status code. For instance, if your route returns a dictionary with the key "message", adjust the test as follows:
If the response does not match expectations—such as returning "Hello World!" (with an exclamation point) instead—the test output will flag the discrepancy:
In another scenario, if the route is accidentally modified to return HTTP status code 201 instead of 200:
The test will capture the mismatch with an error such as:
Maintaining consistent response payloads and correct HTTP status codes is crucial for API reliability. Always update your test cases if you make deliberate changes to your API responses.

Conclusion

In this guide, we covered how to: • Test individual functions, including error handling using Python.
• Leverage FastAPI’s TestClient to simulate HTTP requests, akin to using the Requests library.
• Write robust assertions to verify response payloads and status codes.
By integrating these testing techniques into your development workflow, you can automate and streamline the verification of your FastAPI application, ensuring both routes and business logic perform as expected. Happy Testing!

Watch Video