Skip an RSpec test case at runtime

andrewdotnich picture andrewdotnich · Mar 24, 2011 · Viewed 7.5k times · Source

I'm running RSpec tests against a website product that exists in several different markets. Each market has subtly different combinations of features, etc. I would like to be able to write tests such that they skip themselves at runtime depending on which market/environment they are being run against. The tests should not fail when run in a different market, nor should they pass -- they're simply not applicable.

Unfortunately, there does not seem to be an easy way to mark a test as skipped. How would I go about doing this without trying to inject "pending" blocks (which aren't accurate anyway?)

Answer

Robert Speicher picture Robert Speicher · Mar 24, 2011

Use exclusion filters.

describe "market a", :market => 'a' do
   ...
end
describe "market b", :market => 'b' do
   ...
end
describe "market c", :market => 'c' do
   ...
end

RSpec.configure do |c|
  # Set these up programmatically; 
  # I'm not sure how you're defining which market is 'active'
  c.filter_run_excluding :market => 'a'
  c.filter_run_excluding :market => 'b'
  # Now only tests with ":market => 'c'" will run.
end

Or better still, use implicit filters.

describe "market a", :if => CurrentMarket.a? do # or whatever
   ...
end