How to access tag filters in previous (: suite) / before (: all) hook in RSpec?

I want to access the tag filters passed on the command line

Command line

rspec --tag use_ff 

RSpec configuration

 RSpec.configure do |config| config.before :suite, type: :feature do # how do I check if use_ff filter was specified in the command line? if filter[:use_ff] use_selenium else use_poltergeist end end end 

In hook before(:suite) I want to access the tag filters specified on the command line in config.

According to the rspec-core code base, inclusion tag filters are stored in include_filter RSpec.configuration. Theoretically, I should have access to them as follows:

 RSpec.configure do |config| config.before :suite, type: :feature do if config.filter[:use_ff] # filter is an alias for inclusion_filter use_selenium else use_poltergeist end end end 

But for some reason, I get an empty hash even when passing the tag from the command line.

+5
source share
1 answer

config.filter returns RSpec::Core::InclusionRules . Looking in our superclass RSpec::Core::FilterRules , we see that it has an accessor .rules that returns a tag hash, so you can do, for example,

 RSpec.configure do |config| config.before(:suite) do $running_only_examples_tagged_foo = config.filter.rules[:foo] end end describe "Something" do it "knows we're running only examples tagged foo", :foo do expect($running_only_examples_tagged_foo).to be_truthy # passes end end 

(I am using RSpec 3.4.)

+3
source

Source: https://habr.com/ru/post/1203551/


All Articles