Starlette TestClient

0.4.1 · maintenance · verified Sat Apr 11

starlette-testclient is a backport of Starlette's TestClient that utilizes the `requests` library instead of `httpx`. Its primary goal is to offer a familiar synchronous testing interface for ASGI applications, easing the migration for users accustomed to `requests`-based testing. The current version is 0.4.1. Releases are infrequent, with the latest significant update in April 2024, reflecting a maintenance cadence focused on compatibility rather than rapid feature development, as its purpose is to bridge the gap for users transitioning from older Starlette testing practices.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a basic Starlette application and then test it using `starlette-testclient`'s synchronous `TestClient`.

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from starlette_testclient import TestClient

async def homepage(request):
    return PlainTextResponse("Hello, world!")

routes = [
    Route("/", endpoint=homepage),
]

app = Starlette(routes=routes)

def test_homepage_sync():
    client = TestClient(app)
    response = client.get("/")
    assert response.status_code == 200
    assert response.text == "Hello, world!"

test_homepage_sync()
print("Quickstart test passed!")

view raw JSON →