Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I conditionally skip a scenario in Cucumber?

Tags:

ruby

cucumber

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.

like image 515
BenMQ Avatar asked Mar 10 '14 15:03

BenMQ


5 Answers

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:

Scenario: Test the application
  Given my application has data
  When I test something
  Then I get a result

example step def:

Given /^my application has data$/ do
  pending unless application.has_data?
end

These are the kind of results I can see:

201 scenarios (15 pending, 186 passed)
1151 steps (15 pending, 1136 passed)

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.

like image 97
Ben_Slaughter Avatar answered Oct 06 '22 20:10

Ben_Slaughter


I am tagging my scenarios, and then in my "step_definitions/hooks.rb" file, I have something like this:

Before('@proxy') do 
  skip_this_scenario unless proxy_running?
end

scenario.skip_invoke! which was mentioned in another answer seems to be deprecated.

like image 33
DannyB Avatar answered Sep 28 '22 04:09

DannyB


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:

  • Since the tests and code were implemented, it didn't feel right to mark steps as pending.
  • It caused confusion, as it was difficult to distinguish really pending scenarios from skipped but marked pending scenarios at the end of a run
  • Some CI jobs (Jenkins/Hudson) might be configured to fail for pending scenarios, hence causing more trouble.

So, 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:

  1. Use cucumber.yml to skip tagged scenarios conditionally

Here's a known ignored interesting fact about cucumber (from https://github.com/cucumber/cucumber/wiki/cucumber.yml):

The cucumber.yml file is preprocessed by ERb; this allows you to use ruby code to generate values in the cucumber.yml file

  • 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 condition

    • Now 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.

  1. (More complicated and not elegant) Use rake tasks for cucumber execution:

Conditionally choose tags to include/exclude within your rake task, and pass them to cucumber execution.

Hope this helps.

like image 5
Sam Avatar answered Oct 06 '22 21:10

Sam


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:

default: --tags ~@wip --tags ~@broken --no-source --color 
limited: --tags @core --tags ~@wip --tags ~@broken --no-source --color

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:

cucumber -p limited
like image 3
Dave McNulla Avatar answered Oct 06 '22 20:10

Dave McNulla


Please see this solution which truly skips the scenario instead of trowing a pending error:

Before do |scenario|
      scenario.skip_invoke!
   end
like image 2
Inquisitive Avatar answered Oct 06 '22 21:10

Inquisitive