Cucumber Tag Expressions
cucumber-tag-expressions provides a parser and evaluation logic for boolean tag expressions, commonly used in Cucumber and Behave for filtering scenarios. It is currently at version 9.1.0 and is actively maintained across multiple language implementations, with Python-specific updates occurring as needed.
Warnings
- breaking Version 8.0.0 dropped support for Python versions 2.x up to 3.9. Users must use Python 3.10 or newer.
- gotcha When constructing tag expressions, remember to escape reserved characters (parentheses '()', backslash '\', or whitespace) within a tag name using a backslash.
- gotcha This library implements the current Cucumber Tag Expression syntax. Users migrating from older Cucumber versions (e.g., those using `--tags ~@dev` or `--tags @foo,@bar`) should convert their expressions to the new boolean logic format (e.g., `not @dev` or `@foo or @bar`).
Install
-
pip install cucumber-tag-expressions
Imports
- parse
from cucumber_tag_expressions import parse
Quickstart
from cucumber_tag_expressions import parse
# Define a tag expression
expression_string = "@smoke and not @wip"
# Parse the expression to get an evaluator function
tags_evaluator = parse(expression_string)
# Define a set of tags to test against
scenario_tags_1 = {"@smoke", "@ui"}
scenario_tags_2 = {"@smoke", "@feature", "@not_wip"}
scenario_tags_3 = {"@smoke", "@wip"}
# Evaluate the expression against different sets of tags
result_1 = tags_evaluator(scenario_tags_1)
result_2 = tags_evaluator(scenario_tags_2)
result_3 = tags_evaluator(scenario_tags_3)
print(f"Expression '{expression_string}' against {scenario_tags_1}: {result_1}")
print(f"Expression '{expression_string}' against {scenario_tags_2}: {result_2}")
print(f"Expression '{expression_string}' against {scenario_tags_3}: {result_3}")
# Expected output:
# Expression '@smoke and not @wip' against {'@ui', '@smoke'}: True
# Expression '@smoke and not @wip' against {'@feature', '@not_wip', '@smoke'}: True
# Expression '@smoke and not @wip' against {'@wip', '@smoke'}: False