Skip to main content

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

Project description

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, and per-step detail tabs.

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. Visualizebehave-trace show loads the trace JSON, starts a local HTTP server (stdlib only, no dependencies), and opens the viewer SPA in a browser.

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

# Version
behave-trace --version

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 from CDN)
        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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

behave_trace-1.1.0.tar.gz (91.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

behave_trace-1.1.0-py3-none-any.whl (48.4 kB view details)

Uploaded Python 3

File details

Details for the file behave_trace-1.1.0.tar.gz.

File metadata

  • Download URL: behave_trace-1.1.0.tar.gz
  • Upload date:
  • Size: 91.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for behave_trace-1.1.0.tar.gz
Algorithm Hash digest
SHA256 bc94fa21f8652671a9251798e58a963bcb53e7805afd8b4b96ea6db8abffad03
MD5 cfdcb4980e570f39243441470ca9132e
BLAKE2b-256 3a6a8e0cf2198674c6a026512b88a96c12972ac7495f620e747e36566e3dafbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for behave_trace-1.1.0.tar.gz:

Publisher: release.yml on MathiasPaulenko/behave-trace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file behave_trace-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: behave_trace-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 48.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for behave_trace-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13a5f8e32e83e276235f7797472e13e61b596507136fe78f13334744eb75934f
MD5 3d2abb4b4a71f55f3a3fda7a8699c4a9
BLAKE2b-256 fdf4a6f00fca01f98915345bac51d1f33a8ae209d03a4b50ba8ed2a7b9642ed7

See more details on using hashes here.

Provenance

The following attestation bundles were made for behave_trace-1.1.0-py3-none-any.whl:

Publisher: release.yml on MathiasPaulenko/behave-trace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page