rspecconfigurationrspec3

Configure rspec to exclude a folder unless it is explicitly given to rspec to run


I want to exclude a folder from rspec tests unless it is explicitly given as a command line argument to run. For example

RSpec.configure do |config|

  unless rspec_args.any? { |arg| =~ /shared/ } # do not explicitly run shared folder
    config.exclude_pattern = 'shared/**/*_spec.rb' 
  end

end

I am using rspec v3.9

I have two problems:

  1. Configuring exlude_pattern with config.exclude_pattern = 'shared/**/*_spec.rb' doesn't seem to exclude shared folder from tests to run. But when I run rspec --exclude-pattern shared/**/*_spec.rb shared folder gets excluded from the test suite

  2. How to get arguments passed to rspec from command line in the code?


Solution

  • I was able to achieve this with the following configuration:

    # .rspec
    --require spec_helper
    

    # spec_helper.rb
    RSpec.configure do |config|
      config.exclude_pattern = 'shared/**/*_spec.rb' 
    end