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
¶
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
|
line_length
|
int | None
|
Optional maximum line length for tag wrapping. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Multi-line string representing the complete feature file content. |
Source code in behave_format/printer/feature_printer.py
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
¶
Format a Background block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
background
|
Background
|
The Background to print. |
required |
indent
|
int
|
Base indentation level. |
2
|
language
|
str
|
Feature language for keyword resolution. |
'en'
|
Returns:
| Type | Description |
|---|---|
str
|
Multi-line string with the background and its steps. |
Source code in behave_format/printer/scenario_printer.py
print_scenario
¶
print_scenario(scenario: Scenario, indent: int = 2, *, language: str = 'en', line_length: int | None = None) -> 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
|
language
|
str
|
Feature language for keyword resolution. |
'en'
|
line_length
|
int | None
|
Optional maximum line length for tag wrapping. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Multi-line string with tags, scenario header, and steps. |
Source code in behave_format/printer/scenario_printer.py
print_scenario_outline
¶
print_scenario_outline(outline: ScenarioOutline, indent: int = 2, *, language: str = 'en', line_length: int | None = None) -> 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
|
language
|
str
|
Feature language for keyword resolution. |
'en'
|
line_length
|
int | None
|
Optional maximum line length for tag wrapping. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Multi-line string with tags, outline header, steps, and examples. |
Source code in behave_format/printer/scenario_printer.py
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
¶
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
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
¶
Format a Table as aligned Gherkin table text.
Cell values are re-escaped for Gherkin (| -> \|) and column
widths are computed on the escaped, display-width of each cell so
wide characters (CJK) stay aligned.
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
Tag Printer¶
Formats a list of Tag objects as space-separated lines, wrapping onto
continuation lines when line_length is exceeded.
behave_format.printer.tag_printer
¶
Tag printer — formats tags as space-separated strings.
print_tags
¶
Format a list of tags as one or more space-separated lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tags
|
list[Tag]
|
List of Tag objects. |
required |
indent
|
int
|
Number of spaces to prefix. |
0
|
line_length
|
int | None
|
Optional maximum line length. When exceeded, tags wrap onto continuation lines at the same indent. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A space-separated tag string, or empty string if no tags. |
Source code in behave_format/printer/tag_printer.py
Keywords¶
Resolves the Gherkin keyword to print for each element — the original keyword when the element was parsed from a file, or the canonical i18n alias for the feature's language.
behave_format.printer.keywords
¶
Gherkin keyword resolution.
Elements parsed from a file carry their original keyword in the private
_gherkin_keyword attribute (restored by :mod:behave_format.loader).
Programmatically built models fall back to the first behave i18n alias
for the element's language.
is_known_language
¶
gherkin_keyword
¶
Return the Gherkin keyword to print for an element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Model element (Feature, Rule, Scenario, Examples, ...). |
required | |
kind
|
str
|
behave i18n keyword kind ( |
required |
language
|
str
|
Feature file language code (e.g. |
'en'
|
Returns:
| Type | Description |
|---|---|
str
|
The original keyword if the element was parsed from a file, |
str
|
otherwise the conventional i18n alias for the language. |