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
assertstatements, recorded with their source expression and evaluated values.
Assertion capture
Assertions are captured as the source expression plus the evaluated values, so you see exactly what was compared. For example:
assert response.status_code == 201is 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 are masked, body redaction is configurable, and bodies (along with captured stdout/stderr/caplog streams) are truncated at a 32 KB default. Adjust the inline threshold with --testrelic-pytest-artifact-threshold-kb N.
Opting out
| To skip | Use |
|---|---|
| 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.
Related
Configuration
Complete reference for every testrelic-pytest flag and environment variable: upload strategy, run-type, capture toggles, and metadata.
Reports & Cloud
What testrelic-pytest surfaces in the cloud — the API workspace for rest runs, the Assertions tab, captured streams, and run-type buckets.