Skip a step of behavior in a step implementation

Is there a way to say that you need to perform a step implementation in order to skip the current step?

Something like:

@given("bla bla bla") def step(): skip_current_step() 

A use case is that I want to check if any additional software is installed. If not, I want the full script to be skipped.

+8
python python-behave
source share
3 answers

I don’t know if you can skip inside the step, but you can skip the whole script at the function 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.

+3
source share

Let me improve @Barry's answer:

Basically, what he suggested (i.e. scenario.mark_skipped() ) is:

 scenario.skip(require_not_executed=True) 

To be precise, the mark_skipped() 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 this:

 def skip(self, reason=None, require_not_executed=False) 

A few things:

  • require_not_executed=True means that the script cannot be skipped if any step has already passed, i.e. mark_skipped() at the second or later stage throws an exception and then skips all the steps after that, and not just skips the next steps
  • skip() allows you to specify a skip reason, which is then registered as WARN .

In addition, the scenario object is available in context as context.scenario (next to context.feature ).

Ultimately, a 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 
+8
source share

It depends on the effect with which you will follow. If you want you to skip one step, and only this one step, then from version 1.2.5, the host does not provide an API for this. If you look at behave/model.py you will see that there are no skip and mark_skipped methods for the Step class. There is no alternative mechanism for this.

If the effect you want should behave in order to mark the step and its entire scenario as missing, then this is possible. If you're okay with this, then Barry's answer is what you had to do to behave before 1.2.5: use before_feature to scan the scripts and mark them as skipped before they run. This will effectively mark your step as missed.

As m4tx's answer shows , as of behavior 1.2.5, you can call context.scenario.skip from the step implementation function to skip the script. However, again, this will mean the whole scenario is missing. It is true that the previous steps will be completed and will have a chance to fail, but when the script appears in the account of the skipped scripts and all its steps (including those that could have gone before the step called context.scenario.skip ), it appears in the list of skipped steps. In addition, the steps that follow the step called context.scenario.skip will not be executed at all.

+2
source share

All Articles