Skip to main content

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

Project description

behave-comments

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

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_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.

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
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

# Optional: YAML support
pip install behave-comments[yaml]

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.step)
    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,
    run_before_feature,
    run_after_feature,
    run_before_scenario,
    run_after_scenario,
    run_before_step,
    run_after_step,
)

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)

API Reference

Errors

  • BehaveCommentsError — Base exception
  • ContentTypeError — Unsupported content type
  • ParseError — Parsing failed
  • MissingDependencyError — Optional dependency missing (e.g. pyyaml)
  • 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) — Lifecycle hook declaration

Development

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

License

MIT

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_comments-0.1.0.tar.gz (22.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_comments-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file behave_comments-0.1.0.tar.gz.

File metadata

  • Download URL: behave_comments-0.1.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for behave_comments-0.1.0.tar.gz
Algorithm Hash digest
SHA256 80fcffcde946097fe9fc7b56283c692621d1a237a548d8c0c37b71358619d7ee
MD5 da9cfcc86f1eb3966eba93f3f2ec8a0f
BLAKE2b-256 d3ae1d66c309cf09a507c1ad7c097d7f7021305c25eea2509c7d1cb4c1c8a536

See more details on using hashes here.

Provenance

The following attestation bundles were made for behave_comments-0.1.0.tar.gz:

Publisher: release.yml on MathiasPaulenko/behave-comments

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_comments-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: behave_comments-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for behave_comments-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bda88bb8e8330dc7443f158f3f64350db372835921fe4638bdf2233026d9a61
MD5 1b6a175f1c2430417ebaa5571a5d4db0
BLAKE2b-256 070ba267c6fcd1d4739ee030b125a02b9aa6cf4ab7fb0d22640f61715d6bec06

See more details on using hashes here.

Provenance

The following attestation bundles were made for behave_comments-0.1.0-py3-none-any.whl:

Publisher: release.yml on MathiasPaulenko/behave-comments

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