Skip to content

Printers

Printer modules convert behave-model objects into formatted Gherkin text. Each printer handles a specific model element and is responsible for indentation, alignment, and spacing.

Feature Printer

Formats a complete Feature — including tags, description, background, scenarios, scenario outlines, and rules — into a full .feature file string.

behave_format.printer.feature_printer

Feature printer — formats a complete Feature as .feature text.

print_feature

print_feature(feature: Feature, indent: int = 2) -> str

Format a Feature as valid, deterministic Gherkin text.

Parameters:

Name Type Description Default
feature Feature

The Feature to print.

required
indent int

Base indentation for scenarios and rules.

2

Returns:

Type Description
str

Multi-line string representing the complete feature file content.

Source code in behave_format/printer/feature_printer.py
def print_feature(feature: Feature, indent: int = 2) -> str:
    """Format a Feature as valid, deterministic Gherkin text.

    Args:
        feature: The Feature to print.
        indent: Base indentation for scenarios and rules.

    Returns:
        Multi-line string representing the complete feature file content.
    """
    lines: list[str] = []

    if feature.language and feature.language != "en":
        lines.append(f"# language: {feature.language}")

    for comment in feature.comments:
        lines.append(comment.text)

    if feature.tags:
        lines.append(print_tags(feature.tags, indent=0))

    header = f"Feature: {feature.name}" if feature.name else "Feature:"
    lines.append(header)

    if feature.description:
        for desc_line in feature.description.splitlines():
            if desc_line:
                lines.append(f"{' ' * indent}{desc_line}")
            else:
                lines.append("")

    if feature.background:
        lines.append("")
        lines.append(print_background(feature.background, indent=indent))

    for scenario in feature.scenarios:
        lines.append("")
        if isinstance(scenario, ScenarioOutline):
            lines.append(print_scenario_outline(scenario, indent=indent))
        else:
            lines.append(print_scenario(scenario, indent=indent))

    for rule in feature.rules:
        lines.append("")
        lines.append(_print_rule(rule, indent=indent))

    return "\n".join(lines)

Scenario Printer

Formats Background, Scenario, and ScenarioOutline blocks, including their tags, descriptions, steps, and examples tables.

behave_format.printer.scenario_printer

Scenario printer — formats scenarios and scenario outlines.

print_background

print_background(background: Background, indent: int = 2) -> str

Format a Background block.

Parameters:

Name Type Description Default
background Background

The Background to print.

required
indent int

Base indentation level.

2

Returns:

Type Description
str

Multi-line string with the background and its steps.

Source code in behave_format/printer/scenario_printer.py
def print_background(background: Background, indent: int = 2) -> str:
    """Format a Background block.

    Args:
        background: The Background to print.
        indent: Base indentation level.

    Returns:
        Multi-line string with the background and its steps.
    """
    prefix = " " * indent
    header = "Background:"
    if background.name:
        header = f"Background: {background.name}"
    lines: list[str] = [f"{prefix}{header}"]
    for step in background.steps:
        lines.append(print_step(step, indent=indent + 2))
    return "\n".join(lines)

print_scenario

print_scenario(scenario: Scenario, indent: int = 2) -> str

Format a Scenario as Gherkin text.

Parameters:

Name Type Description Default
scenario Scenario

The Scenario to print.

required
indent int

Base indentation level.

2

Returns:

Type Description
str

Multi-line string with tags, scenario header, and steps.

Source code in behave_format/printer/scenario_printer.py
def print_scenario(scenario: Scenario, indent: int = 2) -> str:
    """Format a Scenario as Gherkin text.

    Args:
        scenario: The Scenario to print.
        indent: Base indentation level.

    Returns:
        Multi-line string with tags, scenario header, and steps.
    """
    prefix = " " * indent
    lines: list[str] = []

    for comment in scenario.comments:
        lines.append(f"{prefix}{comment.text}")

    if scenario.tags:
        lines.append(print_tags(scenario.tags, indent=indent))

    header = f"Scenario: {scenario.name}" if scenario.name else "Scenario:"
    lines.append(f"{prefix}{header}")

    if scenario.description:
        for desc_line in scenario.description.splitlines():
            if desc_line:
                lines.append(f"{prefix}  {desc_line}")
            else:
                lines.append("")

    for step in scenario.steps:
        lines.append(print_step(step, indent=indent + 2))

    return "\n".join(lines)

print_scenario_outline

print_scenario_outline(outline: ScenarioOutline, indent: int = 2) -> str

Format a ScenarioOutline as Gherkin text.

Parameters:

Name Type Description Default
outline ScenarioOutline

The ScenarioOutline to print.

required
indent int

Base indentation level.

2

Returns:

Type Description
str

Multi-line string with tags, outline header, steps, and examples.

Source code in behave_format/printer/scenario_printer.py
def print_scenario_outline(outline: ScenarioOutline, indent: int = 2) -> str:
    """Format a ScenarioOutline as Gherkin text.

    Args:
        outline: The ScenarioOutline to print.
        indent: Base indentation level.

    Returns:
        Multi-line string with tags, outline header, steps, and examples.
    """
    prefix = " " * indent
    lines: list[str] = []

    for comment in outline.comments:
        lines.append(f"{prefix}{comment.text}")

    if outline.tags:
        lines.append(print_tags(outline.tags, indent=indent))

    header = f"Scenario Outline: {outline.name}" if outline.name else "Scenario Outline:"
    lines.append(f"{prefix}{header}")

    if outline.description:
        for desc_line in outline.description.splitlines():
            if desc_line:
                lines.append(f"{prefix}  {desc_line}")
            else:
                lines.append("")

    for step in outline.steps:
        lines.append(print_step(step, indent=indent + 2))

    for examples in outline.examples:
        lines.append("")
        lines.append(_print_examples(examples, indent=indent + 2))

    return "\n".join(lines)

Step Printer

Formats a Step with proper indentation, including attached DocString and data Table.

behave_format.printer.step_printer

Step printer — formats steps with proper indentation.

print_step

print_step(step: Step, indent: int = 4) -> str

Format a single step as Gherkin text.

Parameters:

Name Type Description Default
step Step

The Step to print.

required
indent int

Number of spaces for indentation.

4

Returns:

Type Description
str

Multi-line string with the step and any attached docstring or table.

Source code in behave_format/printer/step_printer.py
def print_step(step: Step, indent: int = 4) -> str:
    """Format a single step as Gherkin text.

    Args:
        step: The Step to print.
        indent: Number of spaces for indentation.

    Returns:
        Multi-line string with the step and any attached docstring or table.
    """
    prefix = " " * indent
    lines: list[str] = []

    for comment in step.comments:
        lines.append(f"{prefix}{comment.text}")

    lines.append(f"{prefix}{step.keyword} {step.name}".rstrip())

    if step.doc_string:
        lines.append(_print_doc_string(step.doc_string, indent))

    if step.data_table:
        lines.append(print_table(step.data_table, indent=indent + 2))

    return "\n".join(lines)

Table Printer

Formats a Table with aligned columns. Computes column widths from headers and all rows to produce a rectangular, padded table.

behave_format.printer.table_printer

Table printer — formats data tables with aligned columns.

print_table

print_table(table: Table, indent: int = 4) -> str

Format a Table as aligned Gherkin table text.

Parameters:

Name Type Description Default
table Table

The Table to print.

required
indent int

Number of spaces for indentation.

4

Returns:

Type Description
str

Multi-line string with aligned table rows.

Source code in behave_format/printer/table_printer.py
def print_table(table: Table, indent: int = 4) -> str:
    """Format a Table as aligned Gherkin table text.

    Args:
        table: The Table to print.
        indent: Number of spaces for indentation.

    Returns:
        Multi-line string with aligned table rows.
    """
    if not table.headers and not table.rows:
        return ""

    widths = _compute_widths(table)
    prefix = " " * indent
    lines: list[str] = []

    header_cells = [
        h.ljust(widths[i]) if i < len(widths) else h for i, h in enumerate(table.headers)
    ]
    lines.append(f"{prefix}| {' | '.join(header_cells)} |")

    for row in table.rows:
        row_cells = [
            cell.ljust(widths[i]) if i < len(widths) else cell for i, cell in enumerate(row.cells)
        ]
        lines.append(f"{prefix}| {' | '.join(row_cells)} |")

    return "\n".join(lines)

Tag Printer

Formats a list of Tag objects as a single space-separated string with optional indentation.

behave_format.printer.tag_printer

Tag printer — formats tags as space-separated strings.

print_tags

print_tags(tags: list[Tag], indent: int = 0) -> str

Format a list of tags as a single line.

Parameters:

Name Type Description Default
tags list[Tag]

List of Tag objects.

required
indent int

Number of spaces to prefix.

0

Returns:

Type Description
str

A space-separated tag string, or empty string if no tags.

Source code in behave_format/printer/tag_printer.py
def print_tags(tags: list[Tag], indent: int = 0) -> str:
    """Format a list of tags as a single line.

    Args:
        tags: List of Tag objects.
        indent: Number of spaces to prefix.

    Returns:
        A space-separated tag string, or empty string if no tags.
    """
    if not tags:
        return ""
    prefix = " " * indent
    return prefix + " ".join(t.name for t in tags)