HTTP API Auto-Capture

Zero-config detection of httpx/requests calls classifies tests as rest API tests and captures requests, responses, and assertions.

The headline feature of testrelic-pytest is zero-config HTTP API auto-detection. Tests that make httpx, requests, or aiohttp calls are automatically classified as api tests and open the API Session Workspace instead of the Web workspace — no markers, decorators, or configuration required.

How detection works

During a test, the plugin observes outbound HTTP traffic from httpx, requests, and aiohttp. If a test makes such calls, it is classified as an api test, and its captured HTTP data is surfaced in the Session Workspace.

What is captured

For each detected request/response, the plugin records:

  • Headers — request and response headers, with sensitive headers masked.
  • Bodies — request and response bodies, truncated at 32 KB by default (configurable redaction). Streamed bodies are never consumed, so capture never disturbs your test.
  • Cookies — structured request and response cookies, with their values masked. They appear in the API workspace Cookies tab.
  • Assertions — real pytest assert statements, recorded with their source expression and evaluated values.
tests/test_api.py::test_post_lifecycleapi · rest — auto-detected
Captured HTTP calls · httpx
No markers or config — httpx traffic classified this test as `api`.
PUThttps://api.example.dev/api/posts/102200 OK63 ms · 290 B
Request
authorizationBearer ●●●●●●●● (masked)content-typeapplication/jsonacceptapplication/json
Response
✕response.status_code == 201200 == 201

A hand-recreated preview of the captured calls for one auto-detected test — select a call on the left, then flip through the request and response tabs (including Test Results, where the assertions land).

Assertion capture

Assertions are captured as the source expression plus the evaluated values, so you see exactly what was compared. For example:

tests/test_api.py
assert response.status_code == 201

is recorded as response.status_code == 201 with evaluated values 200 == 201. The failing assertion is captured too, so a red test carries the comparison that broke it.

Redaction and truncation

Sensitive headers (such as Authorization, Cookie, X-Api-Key) are masked, and built-in redaction patterns (AWS access keys, bearer tokens, private keys, credentials in URLs) are applied to every header value and body. Bodies are truncated at 32 KB. Captured stdout/stderr/caplog streams are also truncated at a 32 KB default, which you can adjust with --testrelic-pytest-artifact-threshold-kb N.

Protocol fixtures

For traffic that isn't plain HTTP — or to record calls explicitly — the plugin provides fixtures that record protocol calls and assertions for the API workspace: testrelic_rest, testrelic_graphql, testrelic_grpc, testrelic_ws (WebSocket), testrelic_kafka, and testrelic_mcp. The transport clients are optional extras:

Terminal
pip install "testrelic-pytest[kafka]"   # or [grpc], [websocket], [graphql], [mcp], [all]
tests/test_orders_stream.py
def test_order_published(testrelic_kafka):
    k = testrelic_kafka
    k.produce("orders.v1", {"id": "ord-42"}, key="ord-42")
    k.consume("orders.v1", value={"id": "ord-42"}, key="ord-42")
    k.assert_message_received("orders.v1", key="ord-42")

Streaming protocols (Kafka, WebSocket, gRPC) also populate the workspace's Streaming panel.

Opting out

To skipUse
HTTP auto-detection--testrelic-pytest-no-autodetect or TESTRELIC_NO_AUTODETECT=1
Request/response bodies--testrelic-pytest-no-capture-bodies or TESTRELIC_NO_CAPTURE_BODIES=1
Request/response headers--testrelic-pytest-no-capture-headers or TESTRELIC_NO_CAPTURE_HEADERS=1
Assertion capture--testrelic-pytest-no-capture-assertions or TESTRELIC_NO_CAPTURE_ASSERTIONS=1

With auto-detection enabled, a plain unit test (no HTTP calls) is reported as a normal test, while an API test is upgraded to the api API Session Workspace automatically.

Was this page helpful?

On this page