You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For detailed documentation on Cucumber Expressions, visit the [main docs](https://github.com/cucumber/cucumber-expressions#readme).
8
+
For detailed documentation on __Cucumber Expressions__, visit the [main docs](https://github.com/cucumber/cucumber-expressions#readme).
9
9
10
-
Cucumber Expressions are presently unsupported natively by common Python frameworks such as [behave](https://behave.readthedocs.io) and [pytest-bdd](https://pytest-bdd.readthedocs.io). However support can be integrated.
10
+
## Installation
11
11
12
-
> For pytest-bdd, see the [pytest-bdd-ng](https://pytest-bdd-ng.readthedocs.io) fork and its documentation on [how to use Cucumber Expressions](https://pytest-bdd-ng.readthedocs.io/en/default/#step-parameters). It should be noted that this fork is out of date with the latest releases.
12
+
__Cucumber Expressions__ is available via [PyPI](https://pypi.org/project/cucumber-expressions/):
13
13
14
-
## Using Cucumber Expressions with Behave
14
+
```console
15
+
pip install cucumber-expressions
16
+
```
15
17
16
-
Native support for Cucumber Expressions in _behave_ is presently unavailable. To enable support, a compatible step matcher must be defined and patched into its matchers. The following steps walk through this process.
18
+
## Usage
17
19
18
-
```console
19
-
pip install cuke4behave
20
-
pip install "cucumber-expressions>17.0.1"
20
+
Cucumber Expressions can be used in numerous contexts as a human-friendly substitution for Regular Expressions.
>>> expression = CucumberExpression("I have {int} cucumbers in my belly", registry)
27
+
>>> expression.regexp
28
+
'^I have ((?:-?\\d+)|(?:\\d+)) cucumbers in my belly$'
29
+
>>> match = expression.match("I have 42 cucumbers in my belly")
30
+
>>> match
31
+
[<cucumber_expressions.argument.Argument object at 0x10dcf7650>]
32
+
>>> argument = match[0]
33
+
>>> argument.value
34
+
42
21
35
```
22
36
23
-
Define a Cucumber Expressions step matcher inside `environment.py` in your `features` directory.
37
+
### Using Cucumber Expressions with Behave
38
+
39
+
Native support for Cucumber Expressions in [behave](https://behave.readthedocs.io) is presently unavailable. To enable support, a compatible step matcher must be defined and patched into Behave's matchers. The following steps walk through this process.
40
+
41
+
Define a Cucumber Expressions step matcher for Behave inside `cucumber_matcher.py` in your `features` directory.
24
42
25
43
```python
26
-
from behave.matchers import matcher_mapping, use_step_matcher
44
+
from behave.matchers import Matcher
45
+
from behave.model_core import Argument
46
+
from cucumber_expressions.expression import CucumberExpression
@given("I am on the profile customisation/settings page")
147
+
defstep_given(context):
79
148
assertTrue
80
149
81
-
82
150
# Reference the parameter type in the step definition pattern
83
-
@when('I select the color "{color}"')
84
-
defstep_when(context, color):
151
+
@when('I select the theme colo(u)r "{color}"')
152
+
defstep_when(context, selected_color):
85
153
assert color
86
-
context.selected_color = color
87
-
154
+
context.selected_color = selected_color
88
155
89
-
@then('the profile color should change to "{color}"')
90
-
defstep_then(context, color):
156
+
@then('the profile colo(u)r should be "{color}"')
157
+
defstep_then(context, displayed_color):
91
158
assert color
92
-
assert context.selected_color == color
159
+
assert context.selected_color == displayed_color
160
+
161
+
```
162
+
163
+
The necessary files are now in place to execute our gherkin scenario.
164
+
165
+
```console
166
+
repository/
167
+
└── features/
168
+
├── steps/
169
+
│ └── color.py
170
+
├── cucumber_matcher.py
171
+
├── environment.py
172
+
└── color.feature
93
173
```
94
174
95
175
Finally, execute Behave. The scenario will run with the step definitions using the Cucumber Expressions parameter type.
96
176
97
177
```console
98
-
➜behave
99
-
Feature: Color selection # features/color.feature:1
178
+
$behave features
179
+
Feature: Color selection # features/Gherkin.feature:1
100
180
Rule: User can select a profile color
101
-
Scenario: User selects a valid color # features/color.feature:5
102
-
Given I am on the profile customization page # features/steps/color.py:21 0.000s
103
-
When I select the color "red" # features/steps/color.py:27 0.000s
104
-
Then the profile color should change to "red" # features/steps/color.py:33 0.000s
181
+
Scenario: User selects a valid color # features/Gherkin.feature:5
182
+
Given I am on the profile settings page # features/steps/color.py:20 0.000s
183
+
When I select the theme colour "red" # features/steps/color.py:26 0.000s
184
+
Then the profile colour should be "red" # features/steps/color.py:32 0.000s
105
185
106
186
1 feature passed, 0 failed, 0 skipped
107
187
1 scenario passed, 0 failed, 0 skipped
@@ -111,4 +191,12 @@ Took 0m0.001s
111
191
112
192
For detailed usage of _behave_, see the [official documentation](https://behave.readthedocs.io).
113
193
114
-
> Note: Alternatives and optionals are currently unsupported with the above example.
194
+
### Using Cucumber Expressions with pytest-bdd
195
+
196
+
Native support for Cucumber Expressions in [pytest-bdd](https://pytest-bdd.readthedocs.io) is presently unavailable. However the [pytest-bdd-ng](https://pytest-bdd-ng.readthedocs.io) fork provides integrated support. Follow its documentation on [how to use Cucumber Expressions](https://pytest-bdd-ng.readthedocs.io/en/default/#step-parameters).
197
+
198
+
> It should be noted that _pytest-bdd-ng_ is out of date with the latest releases of pytest-bdd.
199
+
200
+
## License
201
+
202
+
__Cucumber Expressions__ is licensed under the MIT License.
0 commit comments