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
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.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 exceptionContentTypeError— Unsupported content typeParseError— Parsing failedAnnotationParseError— Invalid annotation syntaxLifecycleStepError— Lifecycle step execution failed
Models
TextBlock(content, content_type, line, parsed)— Parsed doc stringAnnotation(key, value, line, scope, scope_name)— Extracted annotationLifecycleHook(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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file behave_comments-1.0.0.tar.gz.
File metadata
- Download URL: behave_comments-1.0.0.tar.gz
- Upload date:
- Size: 37.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a57b70b537b5a3a9bbe8e5364097c0e04178312f3ca5a830b06ddcc9c68ce7cd
|
|
| MD5 |
719fdff02fb82b5a76bb18d82d5c7e9a
|
|
| BLAKE2b-256 |
363aef2ff3fa23dd6d1a1413fc2d5ccaaf6cfbef0d0ab8f4255e6cfd30cf6db0
|
Provenance
The following attestation bundles were made for behave_comments-1.0.0.tar.gz:
Publisher:
release.yml on MathiasPaulenko/behave-comments
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
behave_comments-1.0.0.tar.gz -
Subject digest:
a57b70b537b5a3a9bbe8e5364097c0e04178312f3ca5a830b06ddcc9c68ce7cd - Sigstore transparency entry: 2182617779
- Sigstore integration time:
-
Permalink:
MathiasPaulenko/behave-comments@b373bc082a8cc09f625f1cbd0f946efb7ee918dc -
Branch / Tag:
refs/heads/master - Owner: https://github.com/MathiasPaulenko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b373bc082a8cc09f625f1cbd0f946efb7ee918dc -
Trigger Event:
push
-
Statement type:
File details
Details for the file behave_comments-1.0.0-py3-none-any.whl.
File metadata
- Download URL: behave_comments-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97f69e6cfb1b2d11806765d31a13ba14ab2204464dca5b2c175d01b2846c765d
|
|
| MD5 |
4edc1d54fdfd25b669a2f3f1fb6dce1b
|
|
| BLAKE2b-256 |
d9fe2e35b8fc1388ecaa79c6375483c36b269ce705a3ee2146b50cff898d089c
|
Provenance
The following attestation bundles were made for behave_comments-1.0.0-py3-none-any.whl:
Publisher:
release.yml on MathiasPaulenko/behave-comments
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
behave_comments-1.0.0-py3-none-any.whl -
Subject digest:
97f69e6cfb1b2d11806765d31a13ba14ab2204464dca5b2c175d01b2846c765d - Sigstore transparency entry: 2182618054
- Sigstore integration time:
-
Permalink:
MathiasPaulenko/behave-comments@b373bc082a8cc09f625f1cbd0f946efb7ee918dc -
Branch / Tag:
refs/heads/master - Owner: https://github.com/MathiasPaulenko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b373bc082a8cc09f625f1cbd0f946efb7ee918dc -
Trigger Event:
push
-
Statement type: