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.
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.
In the step implementation, we shall pass the parameter enclosed in {}. Also, the parameter is passed as one of the arguments to the implementation method. The output shows Shift is: day and Shift is: night printed. Here, the parameters day and night are passed from the step.
You can run a feature file by using -i or --include flags and then the name of the feature file. For more information check the documentation for command line arguments. There's a lot of useful information hidden in their appendix section. NOTE: At the time I'm writing this it won't work with Python 3.6 and Behave 1.2.
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 as WARN
.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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With