@@ -83,6 +83,7 @@ def given(
8383 converters : dict [str , Callable ] | None = None ,
8484 target_fixture : str | None = None ,
8585 stacklevel : int = 1 ,
86+ is_async : bool = False ,
8687) -> Callable :
8788 """Given step decorator.
8889
@@ -95,14 +96,36 @@ def given(
9596
9697 :return: Decorator function for the step.
9798 """
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 )
99121
100122
101123def when (
102124 name : str | StepParser ,
103125 converters : dict [str , Callable ] | None = None ,
104126 target_fixture : str | None = None ,
105127 stacklevel : int = 1 ,
128+ is_async : bool = False ,
106129) -> Callable :
107130 """When step decorator.
108131
@@ -115,14 +138,59 @@ def when(
115138
116139 :return: Decorator function for the step.
117140 """
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 )
119164
120165
121166def then (
122167 name : str | StepParser ,
123168 converters : dict [str , Callable ] | None = None ,
124169 target_fixture : str | None = None ,
125170 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 ,
126194) -> Callable :
127195 """Then step decorator.
128196
@@ -135,7 +203,7 @@ def then(
135203
136204 :return: Decorator function for the step.
137205 """
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 )
139207
140208
141209def step (
@@ -144,6 +212,7 @@ def step(
144212 converters : dict [str , Callable ] | None = None ,
145213 target_fixture : str | None = None ,
146214 stacklevel : int = 1 ,
215+ is_async : bool = False ,
147216) -> Callable [[TCallable ], TCallable ]:
148217 """Generic step decorator.
149218
@@ -168,10 +237,11 @@ def step(
168237 def decorator (func : TCallable ) -> TCallable :
169238 parser = get_parser (name )
170239
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 )
175245
176246 context = StepFunctionContext (
177247 type = type_ ,
0 commit comments