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.
Let me improve @Barry's answer:
Basically, what he proposed (i.e.
scenario.mark_skipped()
) is equal to:To be exact,
mark_skipped()
's source looks like this:skip()
is defined like so: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 ascontext.scenario
(alongsidecontext.feature
).Ultimately, simple example:
Result:
I don't know if you can skip from inside of a step, but you can skip a whole scenario at feature level:
Feature
,Scenario
, andScenarioOutline
all have amark_skipped()
method.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 noskip
andmark_skipped
methods for theStep
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 calledcontext.scenario.skip
) will appear in the list of skipped steps. Moreover, the steps which follow the step that calledcontext.scenario.skip
will not execute at all.