How do I conditionally skip a scenario?
For example, I wish to continue a scenario only if certain conditions are met, but I do not want it to register as a failure if it's not present.
How do I conditionally skip a scenario?
For example, I wish to continue a scenario only if certain conditions are met, but I do not want it to register as a failure if it's not present.
I am tagging my scenarios, and then in my "step_definitions/hooks.rb" file, I have something like this:
scenario.skip_invoke!
which was mentioned in another answer seems to be deprecated.This is an issue I had. The tests I write are against a UI that has a constantly changing BE database that I am currently unable to have static data in. This means that some times it is possible that there is no data for the test. Not a pass not a fail, just unable to run.
The way that I found to work best was to invoke a cucumber pending.
example test:
example step def:
These are the kind of results I can see:
It's worth noting that I have extra debugging and have these tests tagged so that at any time I can run these pending tests again.
Hope this helps, Ben.
Short answer: Why?
Long answer: Cucumber is meant to be used to model the requirements of an end-user (typically customer). The features are meant to detail the list of scenarios in which they are expected to be used in, and the expected results that the features should exhibit when exposed to those scenarios. Those requirements are not conditional - the customer will want them to work all of the time. If Y should not work when X is disabled, then you put that in the Given clause, because that is a Scenario of Y.
Given sets up the state of your Scenario to something known. You are saying, "Given" these known variables, When I do this, Then this should happen. By 'conditionally skipping' a Cucumber clause, you are saying a feature should only work when another feature works.. in which case, why don't you put that in your given clause?
As far as I know, it is possible to skip steps (although I can't remember why), I'm merely questioning why you would want to do that. The only reason I can see you wanting to do this is to say that module A shouldn't work when module B is disabled. But your modules rely on each other then your code is coupled - which is exactly what Cucumber / TDD / BDD encourages you to avoid.
Example I posted in the comments:
This scenario would not hold up if the 'Given' requirements failed. If the 'Given' requirements failed - for example,
Given a spoon and some soup
, you can't possibly expect to end up with two pieces of bread at the end of it. Therefore you are trying to test undefined behaviour. Are you trying to say that if you have a spoon and some soup that cutting bread should no longer work in this universe? No? Then why skip steps?Please see this solution which truly skips the scenario instead of trowing a pending error:
You could check the condition before you start cucumber, then use a profile that would skip the scenarios with certain tags. Put this in your cucumber.yml:
Replace @core with whatever tag you use for the cukes you want to run (or use ~ to exclude cukes). Then run the limited profile from a shell script that checks the conditions:
For anyone still looking for an answer to this: Apart from using
pending
, or a specific profile to skip scenarios with certain tags, there are at least 2 more ways to achieve this.I can understand why you would need this, as I had a similar problem and got a solution, hence worth sharing. In my case, I had a piece of functionality expected to be available on 3/10 devices, and expected to be not available on the remaining 7.
Caveats with using 'pending' to skip:
pending
.pending
scenarios from skipped but markedpending
scenarios at the end of a runSo, I rather wanted to just skip them during execution depending on the condition of which browser is being used. I also didn't want to have too many profiles specific to certain browsers/devices
Solution:
Here's a known ignored interesting fact about cucumber (from https://github.com/cucumber/cucumber/wiki/cucumber.yml):
Building on this, tag your scenarios with something unique, say
@conditional
At the beginning of your cucumber config (cucumber.yml), apply your conditional logic outside of any profiles mentioned:
<% included = (ENV['BROWSER'] =~ /chrome/) ? "-t @conditional" : "-t ~@conditional" %>
included
is just a variable, which will have a value of tags to include/exclude depending on the conditionNow use this conditional variable in the default profile
default: <%= included %>
So now your default profile will use the included/excluded tests as identified by your conditional logic.
Conditionally choose tags to include/exclude within your rake task, and pass them to cucumber execution.
Hope this helps.