Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip a behave step in the step implementation

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.

like image 331
Giacomo d'Antonio Avatar asked Oct 22 '14 09:10

Giacomo d'Antonio


People also ask

How do you skip a step in behave?

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.

How do you pass parameters in behave?

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.

How do I run a specific feature file in behave?

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.


2 Answers

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 steps
  • skip() 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
like image 62
m4tx Avatar answered Nov 12 '22 04:11

m4tx


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.

like image 33
Barry Avatar answered Nov 12 '22 03:11

Barry