> ## Documentation Index
> Fetch the complete documentation index at: https://desme.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# dbg_test

> Wrap a test to capture an execution session and link failures to traced functions.

The `#[dbg_test]` attribute macro wraps a standard Rust test so that its execution is recorded as a dbgflow session. When the test completes — whether it passes or panics — the session is persisted to disk. In the browser UI, the test appears as a node linked to the last traced function it called, making it easy to see exactly where an assertion failure originated.

## Usage

Replace `#[test]` with `#[dbg_test]`, or add it alongside `#[test]`:

```rust theme={null}
use dbgflow::prelude::*;

#[dbg_test]
fn my_test() {
    // test body
}
```

If `#[test]` is not already present, `#[dbg_test]` adds it automatically. You do not need both attributes.

## Arguments

**name** `string`

Optional display label for the test node shown in the browser UI. Defaults to the test function name.

```rust theme={null}
#[dbg_test(name = "Validate pipeline output")]
fn pipeline_output_is_correct() {
    // ...
}
```

## How sessions are saved

`#[dbg_test]` persists the session when the `DBG_SESSION_DIR` environment variable is set to a writable directory path. You do not need to set this variable manually — the `dbgflow test` CLI command sets it automatically before running `cargo test`:

```bash theme={null}
dbgflow test --manifest-path /path/to/Cargo.toml --serve
```

Sessions are written to `artifacts/test-sessions/run-<timestamp>/` by default. Each test that uses `#[dbg_test]` produces one JSON file in that directory. You can override the output directory with `--output-dir`:

```bash theme={null}
dbgflow test --output-dir ./my-sessions
```

> [!NOTE]
>
> If `DBG_SESSION_DIR` is not set, the test still runs normally and the session is captured in memory — it is just not written to disk. This means `#[dbg_test]` is safe to use in any environment, even without the CLI.

## How test nodes are linked in the UI

When a `#[dbg_test]` test calls one or more `#[trace]` functions and then fails, the browser UI draws a `test_link` edge from the test node to the last traced function that was active before the failure. This lets you click the test node and immediately navigate to the function where the failure was rooted.

The test node is color-coded: green for a passing test, red for a failing one.

## Example

The following example shows a test that calls a traced function and fails an assertion:

```rust theme={null}
use dbgflow::prelude::*;

#[ui_debug]
struct Counter {
    value: usize,
}

#[trace]
fn increment(counter: &mut Counter) {
    counter.value += 1;
    counter.emit_snapshot("incremented");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[dbg_test]
    fn counter_increments_twice() {
        let mut c = Counter { value: 0 };
        increment(&mut c);
        increment(&mut c);
        // This assertion fails — the test node will link to `increment` in the UI
        assert_eq!(c.value, 3, "expected 3 but got {}", c.value);
    }
}
```

Run this test through the CLI to capture and immediately inspect the session:

```bash theme={null}
dbgflow test --serve -- counter_increments_twice
```

The UI opens with the failing test node selected and an edge drawn to the `increment` function node.

## Limitation: no async support

> [!WARNING]
>
> `#[dbg_test]` does not support `async` test functions. Applying it to an `async fn` produces a compile error. For async code, run the async runtime manually inside a synchronous test body:
>
> ```rust theme={null}
> #[dbg_test]
> fn async_pipeline_test() {
>     let result = tokio::runtime::Runtime::new()
>         .unwrap()
>         .block_on(async {
>             my_async_function().await
>         });
>     assert!(result.is_ok());
> }
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.