Is there a way to tell behave in a step implementation to skip the current step?
Something like:
@given("bla bla bla")
def step():
skip_current_step()
The use case is that I want to check if some additional software is installed. If not, I want the complete scenario to be skipped.
I don't know if you can skip from inside of a step, but you can skip a whole scenario at feature level:
def before_feature(context, feature):
is_software_installed = some_foo_magic()
if not is_software_installed:
for scenario in feature.scenarios:
if depends_on_software(scenario):
scenario.mark_skipped()
Feature
, Scenario
, and ScenarioOutline
all have a mark_skipped()
method.
Let me improve @Barry's answer:
Basically, what he proposed (i.e. scenario.mark_skipped()
) is equal to:
scenario.skip(require_not_executed=True)
To be exact, mark_skipped()
's source looks like this:
def mark_skipped(self):
"""Marks this scenario (and all its steps) as skipped.
Note that this method can be called before the scenario is executed.
"""
self.skip(require_not_executed=True)
assert self.status == "skipped", "OOPS: scenario.status=%s" % self.status
skip()
is defined like so:
def skip(self, reason=None, require_not_executed=False)
A few things:
require_not_executed=True
means that scenario cannot be skipped if any step has already passed, i.e.mark_skipped()
in second or later step will throw an Exception and then skip all the steps afterwards, instead of just skipping the further stepsskip()
allows to provide a reason for skipping, which is then logged asWARN
.
Furthermore, scenario
object is available in context as context.scenario
(alongside context.feature
).
Ultimately, simple example:
@given("test photos in upload directory")
def step_impl(context):
if not connection_available():
context.scenario.skip(reason='internet connection is unavailable')
Result:
Skipped: skipped
WARNING:behave:SKIP Scenario Retrieving uploaded photo details: internet connection is unavailable
It depends on the effect you are going after. If the effect you want is for behave to skip a single step and only this single step, then as of version 1.2.5 behave does not provide an API to do this. If you look in behave/model.py
you'll see that there are no skip
and mark_skipped
methods for the Step
class. There is no alternate mechanism provided to do this.
If the effect you want is for behave to mark the step and its entire scenario as skipped, then this is possible. If you are okay with this, then the answer by Barry is really what you had to do in behave before 1.2.5: use before_feature
to scan the scenarios and mark them as skipped before running them. This would effectively mark your step as skipped.
As m4tx's answer shows, as of behave 1.2.5 you can call context.scenario.skip
from a step's implementation function to skip the scenario. However, again, this will mark the entire scenario as skipped. It is true that the earlier steps will have executed and will have had a chance to fail but when the scenario will appear in the count of skipped scenarios and all its steps (including those that may have passed before the step that called context.scenario.skip
) will appear in the list of skipped steps. Moreover, the steps which follow the step that called context.scenario.skip
will not execute at all.
来源:https://stackoverflow.com/questions/26504047/skip-a-behave-step-in-the-step-implementation