Skip to main content

behave-comments

PyPI version Python versions CI License

Parse doc strings, extract metadata from comments, and declare lifecycle hooks in Behave .feature files.

Requires Python 3.11+ and behave 1.2.6–1.3.x.

Features

Text Block Parsing

Detect content types in doc strings and parse them automatically:

Given a JSON document
  """json
  {"name": "Alice", "age": 30}
  """
from behave import given
from behave_comments import with_parsed_text

@given("a JSON document")
@with_parsed_text()
def step_given_json(context, text_block):
    data = text_block.parsed  # {"name": "Alice", "age": 30}

Supported content types: json, yaml, xml, csv, form-urlencoded, graphql, text/plain.

The content type can be declared on the doc string opening line ("""json) or as the first line inside the doc string body — both are detected, since Behave itself discards the opening-line media type.

Metadata Annotations

Extract structured metadata from # @key value comments:

# @jira TICKET-42
# @owner team-alpha
Feature: Login

  # @id SC-001
  Scenario: Successful login
    Given the user is on the login page

Annotations attach to the Gherkin element that follows them (feature, scenario, background, rule, examples, or step scope). Comments inside doc strings are ignored, and localized feature files (# language: xx) are supported.

from behave_comments import inject_metadata

def before_feature(context, feature):
    inject_metadata(context, feature)
    # context.metadata == {
    #   "feature": [{"key": "jira", "value": "TICKET-42"}, ...],
    #   "scenario": [{"key": "id", "value": "SC-001"}, ...],
    # }

Lifecycle Hooks

Declare setup/teardown steps directly in comments:

# @before-feature: Given a clean database
Feature: User Tests

  # @after-scenario: Then clear the cache
  Scenario: User login
    Given a registered user
from behave_comments import setup_lifecycle_hooks, run_before_feature, run_after_scenario

def before_feature(context, feature):
    setup_lifecycle_hooks(context, feature)
    run_before_feature(context, feature)

def after_scenario(context, scenario):
    run_after_scenario(context, scenario)

Installation

pip install behave-comments

YAML support is included — pyyaml is a required dependency.

Usage

Text Blocks

from behave_comments import with_parsed_text, extract_text_block

# Option 1: Decorator
@given("a JSON document")
@with_parsed_text()
def step(context, text_block):
    data = text_block.parsed

# Option 2: Manual extraction
def step(context):
    text_block = extract_text_block(context)  # context.text holds the doc string
    data = text_block.parsed

Annotations

from behave_comments import inject_metadata, extract_annotations, annotations_to_tags

def before_feature(context, feature):
    inject_metadata(context, feature)

# Or extract directly
annotations = extract_annotations("features/login.feature")
tags = annotations_to_tags(annotations)

Lifecycle Hooks Usage

from behave_comments import (
    setup_lifecycle_hooks,
    setup_lifecycle_hooks_from_path,
    run_before_all,
    run_after_all,
    run_before_feature,
    run_after_feature,
    run_before_scenario,
    run_after_scenario,
    run_before_step,
    run_after_step,
)

def before_all(context):
    # Accepts a single .feature file or a directory (hooks are collected
    # from every *.feature file inside it, recursively).
    setup_lifecycle_hooks_from_path(context, "features/")
    run_before_all(context)

def after_all(context):
    run_after_all(context)

def before_feature(context, feature):
    setup_lifecycle_hooks(context, feature)
    run_before_feature(context, feature)

def after_feature(context, feature):
    run_after_feature(context, feature)

def before_scenario(context, scenario):
    run_before_scenario(context, scenario)

def after_scenario(context, scenario):
    run_after_scenario(context, scenario)

def before_step(context, step):
    run_before_step(context, step)

def after_step(context, step):
    run_after_step(context, step)

API Reference

Functions

  • extract_text_block(source) — Extract and parse a step's doc string (accepts a step object or context)
  • parse_text(text, content_type) — Parse a text block by content type
  • detect_content_type(opening_line) — Detect the content type from a doc string opening line
  • with_parsed_text(param_name) — Decorator injecting a TextBlock into a step
  • extract_annotations(feature_path) — Extract annotations from a .feature file
  • parse_annotation_line(line, line_number) — Parse a single comment line as an annotation
  • annotations_to_tags(annotations, dedup=False) — Convert annotations to @key-value tag strings
  • inject_metadata(context, feature, prefix="metadata") — Inject annotations into context
  • parse_lifecycle_line(line, line_number) — Parse a single comment line as a hook declaration
  • parse_lifecycle_hooks(feature_path) — Extract all hook declarations from a .feature file
  • setup_lifecycle_hooks(context, feature) / setup_lifecycle_hooks_from_path(context, path) — Load hooks into context
  • run_before_all/after_all/before_feature/after_feature/before_scenario/after_scenario/before_step/after_step — Execute stored hooks

Errors

  • BehaveCommentsError — Base exception
  • ContentTypeError — Unsupported content type
  • ParseError — Parsing failed
  • AnnotationParseError — Invalid annotation syntax
  • LifecycleStepError — Lifecycle step execution failed

Models

  • TextBlock(content, content_type, line, parsed) — Parsed doc string
  • Annotation(key, value, line, scope, scope_name) — Extracted annotation
  • LifecycleHook(hook_type, step_text, line, filename) — Lifecycle hook declaration

Development

pip install -e ".[dev]"
pytest
ruff check behave_comments tests
mypy behave_comments/

License

MIT

Metadata

Release files for behave-comments 1.1.2

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-comments 1.1.2
File Size Uploaded Provenance Actions
behave_comments-1.1.2.tar.gz 46.4 kB With provenance Details for behave_comments-1.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for behave-comments 1.1.2
File Interpreter ABI Platform Actions
behave_comments-1.1.2-py3-none-any.whl Python 3 none any Details for behave_comments-1.1.2-py3-none-any.whl

Total release size: 64.3 kB

Release files / behave_comments-1.1.2.tar.gz

Download URL behave_comments-1.1.2.tar.gz
Size 46.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4d57941979d6cf01793adddac2cfd921d93f247af00a95fc504057d15baaa2dc
BLAKE2b-256 checksum
How to use checksums
057a258ae95693199362c74d0d8e26a7d55b6909fe1a237469552e89d03cda04
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 17, 2026.

Transparency log

Release files / behave_comments-1.1.2-py3-none-any.whl

Download URL behave_comments-1.1.2-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
207987aef421f1a4ca7d85fda0497fb63b22552c9937f19bd61cbf1c1b499281
BLAKE2b-256 checksum
How to use checksums
c2f9fe155e41799dd2b76e7e3329b87ba68cb0d334a8b60ff812fa5756aa27bb
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 17, 2026.

Transparency log

Security for behave-comments 1.1.2

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_comments-1.1.2.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 17, 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