How-To: Test APIs with RestlyTestClient and Fixtures#
FastAPI-Restly includes a test client and pytest fixtures for fast endpoint tests.
Install the testing extra before importing these helpers:
pip install "fastapi-restly[testing]"
The standard extra also includes the testing dependencies.
Client#
from fastapi_restly.testing import RestlyTestClient
client = RestlyTestClient(app)
RestlyTestClient is intentionally sync-only. It still works for testing
async FastAPI routes and AsyncRestView endpoints.
The client auto-asserts default status codes:
getexpects200postexpects201putexpects200patchexpects200deleteexpects204
Override when needed:
client.patch("/users/999", json={}, assert_status_code=404)
Pass assert_status_code=None to skip the assertion entirely and inspect the response yourself.
Pytest Fixtures#
The testing extra installs a pytest plugin entry point, so pytest auto-loads the Restly fixtures. If your project disables plugin autoloading, register the plugin manually:
pytest_plugins = ["fastapi_restly.pytest_fixtures"]
Useful fixtures include:
restly_app— returns a bareFastAPI()instance. You must override this in your ownconftest.pyto return your actual application, otherwise therestly_clientfixture wraps an empty app with no routes.restly_client— aRestlyTestClientwrapping therestly_appfixture.restly_session— a SQLAlchemySessionwith savepoint-based isolation.restly_async_session— same asrestly_sessionbut for async code.
Restly does not register autouse fixtures. If you want database isolation for every test, opt in from your own conftest.py by requesting the fixture there.
Explicit with restly_session.begin(): ... /
async with restly_async_session.begin(): ... blocks are supported and flush
pending changes when the block exits successfully. See
pytest Fixtures Reference for details about the fixture
isolation model.
Example — override restly_app in your conftest.py:
import pytest
from myapp.main import app as myapp
@pytest.fixture
def restly_app():
return myapp
See pytest Fixtures Reference for the full fixture list and isolation model details.
Run Test Suites#
make test-framework
make test-examples
make test-all