Skip to content

Commit a013a11

Browse files
committed
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 6d3fcd7 commit a013a11

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

pytest_bdd/hooks.py

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

2525

26+
def pytest_bdd_step_skip(request, feature, scenario, step, step_func, step_func_args, exception):
27+
"""Called when step function is skipped."""
28+
29+
2630
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
2731
"""Called when step function failed to execute."""
2832

pytest_bdd/plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def pytest_bdd_before_scenario(request, feature, scenario):
6363
reporting.before_scenario(request, feature, scenario)
6464

6565

66+
@pytest.mark.tryfirst
67+
def pytest_bdd_step_skip(request, feature, scenario, step, step_func, step_func_args, exception):
68+
reporting.step_skip(request, feature, scenario, step, step_func, step_func_args, exception)
69+
70+
6671
@pytest.mark.tryfirst
6772
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
6873
reporting.step_error(request, feature, scenario, step, step_func, step_func_args, exception)

pytest_bdd/reporting.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ def fail(self):
156156
report.finalize(failed=True)
157157
self.add_step_report(report)
158158

159+
def skip(self):
160+
"""Stop collecting information and finalize the report as skipped."""
161+
self.current_step_report.finalize(failed=False, skipped=True)
162+
remaining_steps = self.scenario.steps[len(self.step_reports) :]
163+
164+
# Skip the rest of the steps and make reports.
165+
for step in remaining_steps:
166+
report = StepReport(step=step)
167+
report.finalize(failed=False, skipped=True)
168+
self.add_step_report(report)
169+
159170

160171
def runtest_makereport(item, call, rep):
161172
"""Store item in the report object."""
@@ -173,6 +184,11 @@ def before_scenario(request, feature, scenario):
173184
request.node.__scenario_report__ = ScenarioReport(scenario=scenario, node=request.node)
174185

175186

187+
def step_skip(request, feature, scenario, step, step_func, step_func_args, exception):
188+
"""Finalize the step report as skipped."""
189+
request.node.__scenario_report__.skip()
190+
191+
176192
def step_error(request, feature, scenario, step, step_func, step_func_args, exception):
177193
"""Finalize the step report as failed."""
178194
request.node.__scenario_report__.fail()

0 commit comments

Comments
 (0)