@@ -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+ target_exception : str | None = None
6970
7071
7172def get_step_fixture_name (step : Step ) -> str :
@@ -96,6 +97,7 @@ def when(
9697 name : str | StepParser ,
9798 converters : dict [str , Callable ] | None = None ,
9899 target_fixture : str | None = None ,
100+ target_exception : str | None = None ,
99101 stacklevel : int = 1 ,
100102) -> Callable :
101103 """When step decorator.
@@ -104,11 +106,12 @@ def when(
104106 :param converters: Optional `dict` of the argument or parameter converters in form
105107 {<param_name>: <converter function>}.
106108 :param target_fixture: Target fixture name to replace by steps definition function.
109+ :param target_exception: Target exception name to receive Exception object
107110 :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
108111
109112 :return: Decorator function for the step.
110113 """
111- return step (name , WHEN , converters = converters , target_fixture = target_fixture , stacklevel = stacklevel )
114+ return step (name , WHEN , converters = converters , target_fixture = target_fixture , target_exception = target_exception , stacklevel = stacklevel )
112115
113116
114117def then (
@@ -135,6 +138,7 @@ def step(
135138 type_ : Literal ["given" , "when" , "then" ] | None = None ,
136139 converters : dict [str , Callable ] | None = None ,
137140 target_fixture : str | None = None ,
141+ target_exception : str | None = None ,
138142 stacklevel : int = 1 ,
139143) -> Callable [[TCallable ], TCallable ]:
140144 """Generic step decorator.
@@ -143,6 +147,7 @@ def step(
143147 :param type_: Step type ("given", "when" or "then"). If None, this step will work for all the types.
144148 :param converters: Optional step arguments converters mapping.
145149 :param target_fixture: Optional fixture name to replace by step definition.
150+ :param target_exception: Optional target exception name
146151 :param stacklevel: Stack level to find the caller frame. This is used when injecting the step definition fixture.
147152
148153 :return: Decorator function for the step.
@@ -165,6 +170,7 @@ def decorator(func: TCallable) -> TCallable:
165170 parser = parser ,
166171 converters = converters ,
167172 target_fixture = target_fixture ,
173+ target_exception = target_exception ,
168174 )
169175
170176 def step_function_marker () -> StepFunctionContext :
0 commit comments