Skip to main content
Ask AI

HTTP API Auto-Capture

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.

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 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 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
info

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.