pytest
pytest
is a popular testing framework in the Python ecosystem. It provides a simple and powerful way to write tests. FastAPI has excellent support for pytest
, and it can be used for unit, integration, and E2E testing.
TestClient
FastAPI comes with a built-in TestClient
class from the fastapi.testclient
module. It allows you to make requests to your FastAPI application in your tests, similar to how a real client would interact with the API.
First, make sure you have fastapi
and pytest
installed. You can install them using pip
:
pip install fastapi pytest
Let’s create a simple FastAPI application with a single endpoint:
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
pytest
and TestClient
# test_main.py
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
To run the tests, simply execute the following command in your terminal:
pytest
tests
directory at the root of your project and place all your test files inside it./items/{item_id}
, your test function could be named test_read_item
.When testing, you may want to isolate your code from external dependencies such as databases or external APIs. You can use the unittest.mock
library in Python to create mock objects.
from unittest.mock import MagicMock
# Assume we have a function that calls an external API
def get_external_data():
# Code to call external API
pass
# In the test, we can mock the function
mocked_function = MagicMock(return_value={"data": "mocked data"})
# Replace the original function with the mock
get_external_data = mocked_function
Aim for high test coverage, but don’t just focus on the numbers. Make sure your tests are meaningful and cover different scenarios. You can use tools like coverage.py
to measure your test coverage:
pip install coverage
coverage run -m pytest
coverage report
pytest
fixtures are a powerful way to set up and share common resources across multiple tests. For example, you can create a fixture to initialize the TestClient
for your FastAPI application:
import pytest
from fastapi.testclient import TestClient
from main import app
@pytest.fixture
def client():
return TestClient(app)
def test_read_root(client):
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
Integrate your tests into a CI/CD pipeline. Tools like GitHub Actions, GitLab CI/CD, or Jenkins can be used to automatically run your tests whenever there are changes to your codebase. This helps in catching bugs early and ensuring the stability of your application.
Testing is an essential part of developing FastAPI applications. By using testing frameworks like pytest
and the built-in TestClient
, you can write comprehensive tests for your API endpoints. Following common and best practices such as organizing tests, mocking dependencies, aiming for high test coverage, using fixtures, and integrating tests into a CI/CD pipeline will help you build reliable and maintainable FastAPI applications.