@@ -66,6 +66,7 @@ class StepFunctionContext:
66
66
parser : StepParser
67
67
converters : dict [str , Callable [..., Any ]] = field (default_factory = dict )
68
68
target_fixture : str | None = None
69
+ is_async : bool = False
69
70
70
71
71
72
def get_step_fixture_name (step : Step ) -> str :
@@ -78,6 +79,7 @@ def given(
78
79
converters : dict [str , Callable ] | None = None ,
79
80
target_fixture : str | None = None ,
80
81
stacklevel : int = 1 ,
82
+ is_async : bool = False ,
81
83
) -> Callable :
82
84
"""Given step decorator.
83
85
@@ -86,17 +88,62 @@ def given(
86
88
{<param_name>: <converter function>}.
87
89
:param target_fixture: Target fixture name to replace by steps definition function.
88
90
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
91
+ :param is_async: True if the step is asynchronous. (Default: False)
89
92
90
93
:return: Decorator function for the step.
91
94
"""
92
- return step (name , GIVEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
95
+ return step (
96
+ name , GIVEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = is_async
97
+ )
98
+
99
+
100
+ def async_given (
101
+ name : str | StepParser ,
102
+ converters : dict [str , Callable ] | None = None ,
103
+ target_fixture : str | None = None ,
104
+ stacklevel : int = 1 ,
105
+ ) -> Callable :
106
+ """Async Given step decorator.
107
+
108
+ :param name: Step name or a parser object.
109
+ :param converters: Optional `dict` of the argument or parameter converters in form
110
+ {<param_name>: <converter function>}.
111
+ :param target_fixture: Target fixture name to replace by steps definition function.
112
+ :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
113
+
114
+ :return: Decorator function for the step.
115
+ """
116
+ return given (name , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = True )
93
117
94
118
95
119
def when (
96
120
name : str | StepParser ,
97
121
converters : dict [str , Callable ] | None = None ,
98
122
target_fixture : str | None = None ,
99
123
stacklevel : int = 1 ,
124
+ is_async : bool = False ,
125
+ ) -> Callable :
126
+ """When step decorator.
127
+
128
+ :param name: Step name or a parser object.
129
+ :param converters: Optional `dict` of the argument or parameter converters in form
130
+ {<param_name>: <converter function>}.
131
+ :param target_fixture: Target fixture name to replace by steps definition function.
132
+ :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
133
+ :param is_async: True if the step is asynchronous. (Default: False)
134
+
135
+ :return: Decorator function for the step.
136
+ """
137
+ return step (
138
+ name , WHEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = is_async
139
+ )
140
+
141
+
142
+ def async_when (
143
+ name : str | StepParser ,
144
+ converters : dict [str , Callable ] | None = None ,
145
+ target_fixture : str | None = None ,
146
+ stacklevel : int = 1 ,
100
147
) -> Callable :
101
148
"""When step decorator.
102
149
@@ -108,14 +155,15 @@ def when(
108
155
109
156
:return: Decorator function for the step.
110
157
"""
111
- return step (name , WHEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
158
+ return when (name , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = True )
112
159
113
160
114
161
def then (
115
162
name : str | StepParser ,
116
163
converters : dict [str , Callable ] | None = None ,
117
164
target_fixture : str | None = None ,
118
165
stacklevel : int = 1 ,
166
+ is_async : bool = False ,
119
167
) -> Callable :
120
168
"""Then step decorator.
121
169
@@ -124,10 +172,32 @@ def then(
124
172
{<param_name>: <converter function>}.
125
173
:param target_fixture: Target fixture name to replace by steps definition function.
126
174
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
175
+ :param is_async: True if the step is asynchronous. (Default: False)
127
176
128
177
:return: Decorator function for the step.
129
178
"""
130
- return step (name , THEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
179
+ return step (
180
+ name , THEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = is_async
181
+ )
182
+
183
+
184
+ def async_then (
185
+ name : str | StepParser ,
186
+ converters : dict [str , Callable ] | None = None ,
187
+ target_fixture : str | None = None ,
188
+ stacklevel : int = 1 ,
189
+ ) -> Callable :
190
+ """Then step decorator.
191
+
192
+ :param name: Step name or a parser object.
193
+ :param converters: Optional `dict` of the argument or parameter converters in form
194
+ {<param_name>: <converter function>}.
195
+ :param target_fixture: Target fixture name to replace by steps definition function.
196
+ :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
197
+
198
+ :return: Decorator function for the step.
199
+ """
200
+ return step (name , THEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = True )
131
201
132
202
133
203
def step (
@@ -136,6 +206,7 @@ def step(
136
206
converters : dict [str , Callable ] | None = None ,
137
207
target_fixture : str | None = None ,
138
208
stacklevel : int = 1 ,
209
+ is_async : bool = False ,
139
210
) -> Callable [[TCallable ], TCallable ]:
140
211
"""Generic step decorator.
141
212
@@ -144,6 +215,7 @@ def step(
144
215
:param converters: Optional step arguments converters mapping.
145
216
:param target_fixture: Optional fixture name to replace by step definition.
146
217
:param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
218
+ :param is_async: True if the step is asynchronous. (Default: False)
147
219
148
220
:return: Decorator function for the step.
149
221
@@ -165,6 +237,7 @@ def decorator(func: TCallable) -> TCallable:
165
237
parser = parser ,
166
238
converters = converters ,
167
239
target_fixture = target_fixture ,
240
+ is_async = is_async ,
168
241
)
169
242
170
243
def step_function_marker () -> StepFunctionContext :
@@ -177,6 +250,7 @@ def step_function_marker() -> StepFunctionContext:
177
250
f"{ StepNamePrefix .step_def .value } _{ type_ or '*' } _{ parser .name } " , seen = caller_locals .keys ()
178
251
)
179
252
caller_locals [fixture_step_name ] = pytest .fixture (name = fixture_step_name )(step_function_marker )
253
+
180
254
return func
181
255
182
256
return decorator
0 commit comments