@@ -66,6 +66,7 @@ class StepFunctionContext:
6666 parser : StepParser
6767 converters : dict [str , Callable [..., Any ]] = field (default_factory = dict )
6868 target_fixture : str | None = None
69+ is_async : bool = False
6970
7071
7172def get_step_fixture_name (step : Step ) -> str :
@@ -78,6 +79,7 @@ def given(
7879 converters : dict [str , Callable ] | None = None ,
7980 target_fixture : str | None = None ,
8081 stacklevel : int = 1 ,
82+ is_async : bool = False ,
8183) -> Callable :
8284 """Given step decorator.
8385
@@ -86,17 +88,62 @@ def given(
8688 {<param_name>: <converter function>}.
8789 :param target_fixture: Target fixture name to replace by steps definition function.
8890 :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)
8992
9093 :return: Decorator function for the step.
9194 """
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 )
93117
94118
95119def when (
96120 name : str | StepParser ,
97121 converters : dict [str , Callable ] | None = None ,
98122 target_fixture : str | None = None ,
99123 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 ,
100147) -> Callable :
101148 """When step decorator.
102149
@@ -108,14 +155,15 @@ def when(
108155
109156 :return: Decorator function for the step.
110157 """
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 )
112159
113160
114161def then (
115162 name : str | StepParser ,
116163 converters : dict [str , Callable ] | None = None ,
117164 target_fixture : str | None = None ,
118165 stacklevel : int = 1 ,
166+ is_async : bool = False ,
119167) -> Callable :
120168 """Then step decorator.
121169
@@ -124,10 +172,32 @@ def then(
124172 {<param_name>: <converter function>}.
125173 :param target_fixture: Target fixture name to replace by steps definition function.
126174 :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)
127176
128177 :return: Decorator function for the step.
129178 """
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 )
131201
132202
133203def step (
@@ -136,6 +206,7 @@ def step(
136206 converters : dict [str , Callable ] | None = None ,
137207 target_fixture : str | None = None ,
138208 stacklevel : int = 1 ,
209+ is_async : bool = False ,
139210) -> Callable [[TCallable ], TCallable ]:
140211 """Generic step decorator.
141212
@@ -144,6 +215,7 @@ def step(
144215 :param converters: Optional step arguments converters mapping.
145216 :param target_fixture: Optional fixture name to replace by step definition.
146217 :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)
147219
148220 :return: Decorator function for the step.
149221
@@ -165,6 +237,7 @@ def decorator(func: TCallable) -> TCallable:
165237 parser = parser ,
166238 converters = converters ,
167239 target_fixture = target_fixture ,
240+ is_async = is_async ,
168241 )
169242
170243 def step_function_marker () -> StepFunctionContext :
@@ -177,6 +250,7 @@ def step_function_marker() -> StepFunctionContext:
177250 f"{ StepNamePrefix .step_def .value } _{ type_ or '*' } _{ parser .name } " , seen = caller_locals .keys ()
178251 )
179252 caller_locals [fixture_step_name ] = pytest .fixture (name = fixture_step_name )(step_function_marker )
253+
180254 return func
181255
182256 return decorator
0 commit comments