Skip to main content

behave-trace

CI PyPI version Python versions License: MIT Code style: ruff

Trace viewer and step-by-step debugger for Behave BDD.

Captures execution data (steps, statuses, durations, screenshots, DOM snapshots, logs) and visualizes them in a Playwright-inspired web viewer with timeline, filmstrip, per-step detail tabs, before/after DOM diff, and live progress updates.

Quickstart

# 1. Install
pip install behave-trace

# 2. Register the formatter — add to behave.ini in your project root:
#    [behave.formatters]
#    behave-trace = behave_trace.formatter:TraceFormatter

# 3. Capture — run Behave with the formatter
behave --format behave-trace -o trace.json

# 4. Visualize — open the viewer
behave-trace show trace.json

The viewer opens in your browser at http://127.0.0.1:<port> with a dark-themed SPA showing features, scenarios, steps, screenshots, and errors.

How it works

┌──────────┐     ┌──────────────┐     ┌───────────┐     ┌──────────────┐
│  Behave  │────▶│  Formatter   │────▶│  Serializer │────▶│  trace.json  │
│  runner  │     │  (collector) │     │  (JSON)    │     │              │
└──────────┘     └──────────────┘     └───────────┘     └──────┬───────┘
                                                            │
                   ┌────────────────────────────────────────┘
                   ▼
            ┌──────────────┐     ┌──────────────────┐
            │  behave-trace │────▶│  Browser SPA     │
            │  show         │     │  (Alpine.js)     │
            │  (HTTP server)│     │  Dark theme      │
            └──────────────┘     └──────────────────┘

Two-phase model (like Playwright Trace Viewer):

  1. Capture — The TraceFormatter hooks into Behave's formatter API and collects execution events into a Trace data model. Attachments (screenshots, DOM, logs) are captured via attach_screenshot(), attach_dom(), and log() helpers in environment.py.

  2. Visualize — behave-trace show loads the trace JSON, starts a local HTTP server (stdlib only, no dependencies), and opens the viewer SPA in a browser.

Viewer features

  • Visual DOM diff — compare before/after DOM snapshots with added and removed elements highlighted in split, diff, or before/after view modes.
  • Collapse/Expand all — expand or collapse the entire feature tree with one click.
  • Scenario sorting — sort scenarios by name, duration, or status; the selected sort is persisted in localStorage.
  • Feature → Scenario breadcrumb — shows the current feature and scenario above the step list; clicking the feature opens it in the sidebar.
  • Live progress — real-time status updates via Server-Sent Events while tests are running from the viewer.
  • Theme and state persistence — theme and scenario sort order are remembered across sessions (localStorage).
  • Port conflict detection — clear error message instead of hanging when the requested port is already in use (especially on Windows).

Capturing attachments

Add to your environment.py:

from behave_trace import attach_screenshot, attach_dom, attach_text, attach_network, log

def after_step(context, step):
    # Log the current URL after every step
    log(context, f"URL: {context.driver.current_url}")

    if step.status == "failed":
        attach_screenshot(context, context.driver, name="failure.png")
        attach_dom(context, context.driver, name="dom.html")
        log(context, f"Step failed: {step.name}", level="error")

The viewer will show screenshots in the filmstrip and detail tabs, with before/after DOM snapshot toggling. See the attachments guide for the full API.

CLI

# Show trace in browser
behave-trace show trace.json

# Show on specific port, don't open browser
behave-trace show trace.json --port 8080 --no-browser

# Run behave with the trace formatter, then open the viewer
behave-trace run features/

# Run with tags and watch mode
behave-trace run features/ --tags @smoke --watch

# Run from the UI (no initial trace required)
# The viewer opens even if behave fails. A "Run all" button lets you
# execute tests from scratch; selected scenarios can also be re-run.
behave-trace run --no-browser

# Version
behave-trace --version

Run from the UI

behave-trace run starts the viewer even if the initial Behave run fails or no features have been executed yet. The viewer then shows a "Run all" button in the header (and in the empty state) that triggers a fresh Behave execution (POST /api/run).

While tests are running the viewer receives real-time status updates via Server-Sent Events (SSE), and the trace is reloaded automatically when the run finishes. You can also select one or more scenarios and re-run only those with "Run selected".

When watch mode is active, the viewer header shows an "Auto-run" toggle. Enable it to re-execute Behave automatically whenever the watched *.feature or *.py files change (debounced 500ms). Disable it to keep the viewer running and reload the trace manually.

Development

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Or use the Makefile shortcut
make dev

# Lint
ruff check .
ruff format --check .

# Type check
mypy --strict behave_trace

# Run tests
pytest tests/ -v

# E2E tests (meta: Behave testing Behave)
behave tests/e2e/

# Build
python -m build

Requirements

  • Python 3.11+ (tested on 3.11, 3.12, 3.13)
  • behave >= 1.2.6 (installed automatically)
  • No other runtime dependencies (viewer uses only Python stdlib)

Project structure

behave_trace/
    __init__.py          # Public API, formatter registration
    __main__.py          # python -m behave_trace entry point
    formatter.py         # Behave formatter (TraceFormatter)
    collector.py         # Event collector → Trace model
    models.py            # Dataclasses: Trace, Feature, Scenario, Step, etc.
    serializer.py        # JSON load/save
    attach.py            # Attachment helpers (screenshot, DOM, text, network, log)
    runner.py            # Behave runner (subprocess wrapper)
    watcher.py           # File watcher for --watch mode
    utils.py             # Utilities (format_duration, safe_str)
    cli/
        app.py           # argparse CLI with `show` and `run` subcommands
    viewer/
        server.py        # stdlib HTTP server (ThreadingHTTPServer)
        browser.py       # Browser opener (Chrome app mode)
    assets/
        index.html       # SPA shell (Alpine.js vendored locally)
        css/viewer.css   # Dark theme styles
        js/viewer.js     # Alpine.js component logic

Documentation

Full documentation is available at mathiaspaulenko.github.io/behave-trace.

Contributing

Contributions are welcome! See CONTRIBUTING.md for setup, commands, and the release process.

License

MIT — see LICENSE.

Metadata

Release files for behave-trace 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for behave-trace 1.4.1
File Size Uploaded Provenance Actions
behave_trace-1.4.1.tar.gz 169.4 kB With provenance Details for behave_trace-1.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for behave-trace 1.4.1
File Interpreter ABI Platform Actions
behave_trace-1.4.1-py3-none-any.whl Python 3 none any Details for behave_trace-1.4.1-py3-none-any.whl

Total release size: 249.0 kB

Release files / behave_trace-1.4.1.tar.gz

Download URL behave_trace-1.4.1.tar.gz
Size 169.4 kB
Tags Source
SHA-256 checksum
How to use checksums
dd5e05b7b093a6daf5aaa88abd8824542d1ba1c4e69109983da51f81e327e138
BLAKE2b-256 checksum
How to use checksums
43d337f746927f4eb17bec595a66a57c91e1bd4568f4838260b154590afb17ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / behave_trace-1.4.1-py3-none-any.whl

Download URL behave_trace-1.4.1-py3-none-any.whl
Size 79.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50ff9b833d23c26b419dd4b5fc4a6410251c92f53639daaa9638055e0f8e0672
BLAKE2b-256 checksum
How to use checksums
f8f42acb83913aa4371c7c122c82b83f2cc1528cf42cc96ac2f0ef5079351f53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Security for behave-trace 1.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for behave_trace-1.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page