Skip to content

Commit a2e48b1

Browse files
dsax7hristiy4n
authored andcommitted
Add hook and plugin to modify steps when skipped
- the hook and the pluging work in a similar way to the fail hook and plugin where they set a flag wether the step is skipped or not
1 parent 8897a41 commit a2e48b1

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/pytest_bdd/hooks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func
2525
"""Called after step function is successfully executed."""
2626

2727

28+
def pytest_bdd_step_skip(request, feature, scenario, step, step_func, step_func_args, exception):
29+
"""Called when step function is skipped."""
30+
31+
2832
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
2933
"""Called when step function failed to execute."""
3034

src/pytest_bdd/plugin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ def pytest_bdd_before_scenario(request: FixtureRequest, feature: Feature, scenar
8787
reporting.before_scenario(request, feature, scenario)
8888

8989

90+
@pytest.hookimpl(tryfirst=True)
91+
def pytest_bdd_step_skip(
92+
request: FixtureRequest,
93+
feature: Feature,
94+
scenario: Scenario,
95+
step: Step,
96+
step_func: Callable,
97+
step_func_args: dict,
98+
exception: Exception,
99+
) -> None:
100+
reporting.step_skip(request, feature, scenario, step, step_func, step_func_args, exception)
101+
102+
90103
@pytest.hookimpl(tryfirst=True)
91104
def pytest_bdd_step_error(
92105
request: FixtureRequest,

src/pytest_bdd/reporting.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ def fail(self) -> None:
138138
report.finalize(failed=True)
139139
self.add_step_report(report)
140140

141+
def skip(self):
142+
"""Stop collecting information and finalize the report as skipped."""
143+
self.current_step_report.finalize(failed=False, skipped=True)
144+
remaining_steps = self.scenario.steps[len(self.step_reports) :]
145+
146+
# Skip the rest of the steps and make reports.
147+
for step in remaining_steps:
148+
report = StepReport(step=step)
149+
report.finalize(failed=False, skipped=True)
150+
self.add_step_report(report)
151+
141152

142153
def runtest_makereport(item: Item, call: CallInfo, rep: TestReport) -> None:
143154
"""Store item in the report object."""
@@ -155,6 +166,18 @@ def before_scenario(request: FixtureRequest, feature: Feature, scenario: Scenari
155166
request.node.__scenario_report__ = ScenarioReport(scenario=scenario)
156167

157168

169+
def step_skip(
170+
request: FixtureRequest,
171+
feature: Feature,
172+
scenario: Scenario,
173+
step: Step,
174+
step_func: Callable,
175+
step_func_args: dict,
176+
exception: Exception,
177+
) -> None:
178+
"""Finalize the step report as skipped."""
179+
request.node.__scenario_report__.skip()
180+
158181
def step_error(
159182
request: FixtureRequest,
160183
feature: Feature,

0 commit comments

Comments
 (0)