How do you get cucumber/guard to filter on tags like @wip?

大憨熊 提交于 2019-12-01 17:13:01

You could use a cucumber profile to define the tags that you want to execute. Using the YML file, you can define a profile that execute your @wip tags:

wip: --tags @wip

More info at:

https://github.com/cucumber/cucumber/wiki/cucumber.yml

You can also just run cucumber from the command line and pass it the -t argument:

cucumber -t @wip,@now

From the help (cucumber -h):

Only execute the features or scenarios with tags matching TAG_EXPRESSION. Scenarios inherit tags declared on the Feature level. The simplest TAG_EXPRESSION is simply a tag. Example: --tags @dev. When a tag in a tag expression starts with a ~, this represents boolean NOT. Example: --tags ~@dev. A tag expression can have several tags separated by a comma, which represents logical OR. Example: --tags @dev,@wip. The --tags option can be specified several times, and this represents logical AND. Example: --tags @foo,~@bar --tags @zap. This represents the boolean expression (@foo || !@bar) && @zap

Hence, in theory we can use the guardfile with these options:

guard 'cucumber', :cli => "--drb --tags @now" do
  watch(%r{^features/.+\.feature$})
  ...
end

An important concept to understand is there is a difference between tags and profiles. I am also using Guard with Cucumber and was frustrated that the default profile continued to be used and none of my @wip (Work In Progress) tags were being picked up. It's obvious now why that was the case. As stated by some in other forums, my default profile filters out @wip.

<config/cucumber.yml>

<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
base_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'}"
std_opts = "#{base_opts} --strict --tags ~@wip"
wip_opts = base_opts
%>
default: --drb <%= std_opts %> features
wip: --drb <%= wip_opts %> --tags @wip:3 --wip features
rerun: --drb <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip

"std_opts = "#{base_opts} --strict --tags ~@wip" <= wip is filtered out here in std_opts

I want to use the 'wip' profile, which would include scenarios or features marked by '@wip'!

wip: --drb <%= wip_opts %> --tags @wip:3 --wip features" <= the number represents the max number of scenarios to run; '--wip' indicates that Cuc expects the test will fail (because we're working on it)

So the tags are already configured and I've included '@wip' in my *.feature file. What about the profiles? When using Guard (Spork), in order for the 'wip' profile to be used, it needs to be configured. It makes sense; the computer can't read my mind! Update the Guardfile to use the 'wip' profile.

<Guardfile>

guard 'cucumber', :cli => "--drb -p wip", :all_on_start => false, :all_after_pass => false do
  watch(%r{^features/.+\.feature$})
  watch(%r{^features/support/.+$})          { 'features' }
  watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
end

guard 'cucumber', :cli => "--drb -p wip" <= '-p' to specify desired profile

And now my scenarios are successfully being filtered by 'wip'.

Not sure when this option was introduced but guard-cucumber has the ability to focus on a particular tag (which is different than hard-coding a specific tag to always filter by). You can leave this configuration option in your Guardfile and only use your focus tag when you need it:

# Guardfile
guard 'cucumber', :focus_on => 'myfocustag' do
  ...
end

# example.feature
Feature: Example

  @myfocustag
  Scenario: Only run this one
  ...

cucumber-guard will then filter these scenarios before passing them to the cucumber command. Removing these tags would result in the default behavior (running all scenarios, rather than none).

Although in theory it should be possible to make this work using cucumber profiles I found I had to use the guardfile.

Original guardfile

guard 'cucumber', :cli => "--drb" do
  watch(%r{^features/.+\.feature$})
  ...
end

modified guardfile

guard 'cucumber', :cli => "--drb --tags @now" do
  watch(%r{^features/.+\.feature$})
  ...
end

Now if you want Guard to always run @wip like me then in your add:

cucumber.yml

guard: --format pretty --tags @wip

Guardfile

guard 'cucumber', :command_prefix => 'spring', :cli => '--profile guard', :bundler => false do
  # your watches
end

that a watched file is modified then only @wip is going to be run but also when you type cucumber in the guard console.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!