Query API Guide¶
The Query API provides high-level filtering functions to find features, scenarios, steps, and tags without manually traversing the tree.
Finding features¶
By name¶
By tag¶
Finding scenarios¶
By tag¶
By exact name¶
By name substring¶
Combined filters¶
# Scenarios with @smoke tag AND name containing "login"
for s in project.find_scenarios(tag="@smoke", name_contains="login"):
print(s.name)
All scenarios (including those in Rules)¶
Scenario Outlines only¶
from behave_model import ScenarioOutline
for outline in project.find_outlines():
print(f"Outline: {outline.name}")
for examples in outline.examples:
print(f" Examples: {examples.name or '(unnamed)'}")
Plain Scenarios only¶
Finding steps¶
By keyword¶
given_steps = project.find_steps(keyword="Given")
when_steps = project.find_steps(keyword="When")
then_steps = project.find_steps(keyword="Then")
print(f"Given: {len(given_steps)}")
print(f"When: {len(when_steps)}")
print(f"Then: {len(then_steps)}")
By text substring¶
Combined¶
Finding tags¶
By name¶
All tags¶
Standalone query functions¶
All query methods on Project are also available as standalone functions:
from behave_model import (
find_feature,
find_features_with_tag,
find_scenarios,
find_scenarios_with_tag,
find_steps,
find_tag,
find_outlines,
find_plain_scenarios,
)
# These work the same as the Project methods
for f in find_features_with_tag(project, "@smoke"):
print(f.name)
for s in find_scenarios(project, tag="@api"):
print(s.name)
Practical examples¶
Count scenarios per feature¶
for feature in project.features:
total = len(feature.scenarios)
for rule in feature.rules:
total += len(rule.scenarios)
print(f"{feature.name}: {total} scenarios")
Find untagged scenarios¶
Find scenarios lacking an effective tag¶
find_scenarios(tag=...) matches inherited tags, so the inverse finds
scenarios with no tag at any level:
tagged = {id(s) for s in project.find_scenarios(tag="@regression")}
for s in project.all_scenarios():
if id(s) not in tagged:
print(f" Not in @regression: {s.name}")
Find duplicate scenario names¶
from collections import Counter
names = Counter(s.name for s in project.all_scenarios())
for name, count in names.most_common():
if count > 1:
print(f" Duplicate: '{name}' ({count} times)")
Next steps¶
- Visitors — For complex traversal logic
- Statistics — Built-in metrics
- API Reference — Model — Complete query API