> ## Documentation Index
> Fetch the complete documentation index at: https://docs.021labs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing

> Write tests for LLM applications without making real API calls

## Overview

RDK provides several mechanisms for testing LLM applications:

* `mode="test"` — disables HTTP transport (null transport)
* `enabled=False` — disables tracing entirely
* `capture_traces()` — captures spans in memory for assertions

## Test mode

`mode="test"` sets `transport="null"` — no HTTP calls are made.

```python theme={null}
from rdk import init

init(mode="test")
```

Also reads from `RDK_MODE=test` environment variable.

## enabled=False

When you don't want any tracing at all:

```python theme={null}
from rdk import init

init(enabled=False)  # init() returns None, no instrumentation occurs
```

## pytest fixture

A standard fixture that initializes RDK in test mode and tears down after each test:

```python theme={null}
# conftest.py
import pytest
from rdk import init, shutdown

@pytest.fixture(autouse=True)
def rdk():
    init(mode="test")
    yield
    shutdown()
```

## capture\_traces()

Assert on the spans that were created during a function call:

```python theme={null}
from rdk.testing import capture_traces
from rdk import observe
from anthropic import Anthropic

@observe()
def chat(message: str) -> str:
    client = Anthropic()
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=256,
        messages=[{"role": "user", "content": message}],
    )
    return response.content[0].text

def test_chat_creates_span():
    with capture_traces() as spans:
        chat("Hello")

    assert len(spans) == 1
    assert spans[0].name == "anthropic.messages.create"
    assert spans[0].metadata["provider"] == "anthropic"
```

### Check token usage

```python theme={null}
def test_token_usage_captured():
    with capture_traces() as spans:
        chat("Hello")

    llm_span = spans[0]
    assert llm_span.token_usage is not None
    assert llm_span.token_usage.total_tokens > 0
```

## InMemoryTransport

For lower-level assertions, use `InMemoryTransport` directly:

```python theme={null}
from rdk import init
from rdk.testing import InMemoryTransport

def test_with_memory_transport():
    transport = InMemoryTransport()
    client = init(mode="test")
    client.set_transport(transport)

    chat("Hello")
    client.flush()

    assert transport.sent_count == 1
    assert len(transport.spans) == 1
```

## Environment variable approach

Set `RDK_MODE=test` in your test environment to enable test mode without changing code:

```bash theme={null}
# pytest.ini or pyproject.toml
[pytest]
env =
    RDK_MODE=test
```

Or in a shell:

```bash theme={null}
RDK_MODE=test pytest
```

## See Also

* [init()](/api-reference/init) — `mode` and `enabled` parameters
