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

旧巷老猫 提交于 2019-12-19 18:49:26

问题


I'm running spork and guard and all has been going very well with my RSpec tests which were all run correctly. In order to speed up the tests I could successfully filter my RSpec tests with tags I placed in my .rspec file.

.rspec

--colour
--debug
--tag focus
--tag now

Unfortunately though I have not been able to filter my cucumber tags. Every time cucumber runs it runs either everything or just the file that changed.

How can I get cucumber/spork/guard to respect tags like @wip, @now etc and run only those tests? Is there some equivalent to the .rspec file for cucumber tags?


回答1:


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



回答2:


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'.




回答3:


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).




回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/9193120/how-do-you-get-cucumber-guard-to-filter-on-tags-like-wip

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