Skip to main content
The Python SDK ships a dedicated trulayer.testing module with an in-memory sender and a fluent assertion chain. No API key is required, no network calls are made, and the helpers work under pytest, unittest, or any runner that treats AssertionError as a failure.

Install

The testing helpers are bundled with the main package; import them from the submodule so production code stays free of test-only symbols:

Write your first test

API

create_test_client(**overrides)

Returns a (client, sender) tuple. The client is a fully functional TruLayerClient wired to an in-memory LocalBatchSender. Pass keyword overrides (sample_rate, redact, project) to exercise specific client behavior:

assert_sender(sender)

Entry point for the fluent assertion chain. Each method returns self so assertions chain naturally.

Replay captured traces

LocalBatchSender.flush_to_file(path) serializes every captured trace to a JSONL file — one JSON object per line. Combined with TRULAYER_MODE=replay, this enables golden-file regression tests and reproducing production traces locally.
Malformed JSONL lines are skipped with a warnings.warn — the helper follows the SDK’s never-throws contract so a single corrupt line in a fixture never takes down an entire test run.

Running captures via environment variables

For integration tests that spin up a full app, set the mode variables on the process before loading your code. trulayer.init() wires them up automatically:
Replay a previously captured fixture through the SDK as if it were live traffic:
TRULAYER_MODE=replay implies local — replayed traces never escape to the live API, because they were produced by a previous capture and would double-count.

See also

  • Failure behavior — how the SDK handles ingest outages and how to opt in to block mode.
  • Python SDK reference — full signatures for create_test_client, assert_sender, and LocalBatchSender.