@@ -83,6 +83,7 @@ def given(
83
83
converters : dict [str , Callable ] | None = None ,
84
84
target_fixture : str | None = None ,
85
85
stacklevel : int = 1 ,
86
+ is_async : bool = False ,
86
87
) -> Callable :
87
88
"""Given step decorator.
88
89
@@ -95,14 +96,36 @@ def given(
95
96
96
97
:return: Decorator function for the step.
97
98
"""
98
- return step (name , GIVEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
99
+ return step (
100
+ name , GIVEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = is_async
101
+ )
102
+
103
+
104
+ def async_given (
105
+ name : str | StepParser ,
106
+ converters : dict [str , Callable ] | None = None ,
107
+ target_fixture : str | None = None ,
108
+ stacklevel : int = 1 ,
109
+ ) -> Callable :
110
+ """Async Given step decorator.
111
+
112
+ :param name: Step name or a parser object.
113
+ :param converters: Optional `dict` of the argument or parameter converters in form
114
+ {<param_name>: <converter function>}.
115
+ :param target_fixture: Target fixture name to replace by steps definition function.
116
+ :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
117
+
118
+ :return: Decorator function for the step.
119
+ """
120
+ return given (name , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = True )
99
121
100
122
101
123
def when (
102
124
name : str | StepParser ,
103
125
converters : dict [str , Callable ] | None = None ,
104
126
target_fixture : str | None = None ,
105
127
stacklevel : int = 1 ,
128
+ is_async : bool = False ,
106
129
) -> Callable :
107
130
"""When step decorator.
108
131
@@ -115,14 +138,59 @@ def when(
115
138
116
139
:return: Decorator function for the step.
117
140
"""
118
- return step (name , WHEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
141
+ return step (
142
+ name , WHEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = is_async
143
+ )
144
+
145
+
146
+ def async_when (
147
+ name : str | StepParser ,
148
+ converters : dict [str , Callable ] | None = None ,
149
+ target_fixture : str | None = None ,
150
+ stacklevel : int = 1 ,
151
+ ) -> Callable :
152
+ """When step decorator.
153
+
154
+ :param name: Step name or a parser object.
155
+ :param converters: Optional `dict` of the argument or parameter converters in form
156
+ {<param_name>: <converter function>}.
157
+ :param target_fixture: Target fixture name to replace by steps definition function.
158
+ :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
159
+ :param is_async: True if the step is asynchronous. (Default: False)
160
+
161
+ :return: Decorator function for the step.
162
+ """
163
+ return when (name , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = True )
119
164
120
165
121
166
def then (
122
167
name : str | StepParser ,
123
168
converters : dict [str , Callable ] | None = None ,
124
169
target_fixture : str | None = None ,
125
170
stacklevel : int = 1 ,
171
+ is_async : bool = False ,
172
+ ) -> Callable :
173
+ """Then step decorator.
174
+
175
+ :param name: Step name or a parser object.
176
+ :param converters: Optional `dict` of the argument or parameter converters in form
177
+ {<param_name>: <converter function>}.
178
+ :param target_fixture: Target fixture name to replace by steps definition function.
179
+ :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
180
+ :param is_async: True if the step is asynchronous. (Default: False)
181
+
182
+ :return: Decorator function for the step.
183
+ """
184
+ return step (
185
+ name , THEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = is_async
186
+ )
187
+
188
+
189
+ def async_then (
190
+ name : str | StepParser ,
191
+ converters : dict [str , Callable ] | None = None ,
192
+ target_fixture : str | None = None ,
193
+ stacklevel : int = 1 ,
126
194
) -> Callable :
127
195
"""Then step decorator.
128
196
@@ -135,7 +203,7 @@ def then(
135
203
136
204
:return: Decorator function for the step.
137
205
"""
138
- return step (name , THEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
206
+ return step (name , THEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel , is_async = True )
139
207
140
208
141
209
def step (
@@ -144,6 +212,7 @@ def step(
144
212
converters : dict [str , Callable ] | None = None ,
145
213
target_fixture : str | None = None ,
146
214
stacklevel : int = 1 ,
215
+ is_async : bool = False ,
147
216
) -> Callable [[TCallable ], TCallable ]:
148
217
"""Generic step decorator.
149
218
@@ -168,10 +237,11 @@ def step(
168
237
def decorator (func : TCallable ) -> TCallable :
169
238
parser = get_parser (name )
170
239
171
- if inspect .isasyncgenfunction (func ):
172
- func = wrap_asyncgen (func )
173
- elif inspect .iscoroutinefunction (func ):
174
- func = wrap_coroutine (func )
240
+ if is_async :
241
+ if inspect .isasyncgenfunction (func ):
242
+ func = wrap_asyncgen (func )
243
+ elif inspect .iscoroutinefunction (func ):
244
+ func = wrap_coroutine (func )
175
245
176
246
context = StepFunctionContext (
177
247
type = type_ ,
0 commit comments